导致以下函数错误的 c++ 模板
c++ Templates causing errors with function below
>我刚刚开始学习C ++,我一直试图弄清楚为什么我会遇到这个问题,在使用标准数据结构(使用int)之前代码工作正常,但是一旦我尝试使用模板反而遇到了问题
希望我在正确的部分发布了这个,如果需要,我会发布更多代码
83 template <class t>
84 struct node
85 {
86 t number ;
87 node *next ;
88 };
89
90 bool isEmpty(node *head)
91 {
92 if (head == NULL)
93 {
94 return true;
95 }
96 else
97 {
98 return false;
99 }
100 }
错误我得到.
91|error: missing template arguments before '*' token|
91|error: 'head' was not declared in this scope|
92|error: expected ',' or ';' before '{' token|
||=== Build finished: 3 errors, 0 warnings (0 minutes, 0 seconds) ===|
感谢我得到的任何反馈:)
你需要 s.th,比如:
template<class t>
bool isEmpty(node<t> *head) ...
note
是一个模板,你需要用一个类型来实例化它,比如
bool isEmpty(node<int> *head)
{
return head == NULL; // compare to if/else, this is much neater, right?
}
或isEmpty
模板函数
template<typename T>
bool isEmpty(node<T> *head)
{
return head == NULL;
}
相关文章:
- .cpp和.h文件中的模板专用化声明
- C++模板来检查友元函数的存在
- 如何使用默认参数等选择模板专业化
- 模板参数替换失败,并且未完成隐式转换
- 具有默认模板参数的多态类的模板推导失败
- 部分定义/别名模板模板参数
- 模板-模板参数推导:三个不同的编译器三种不同的行为
- 具有奇怪重复模板模式的派生类中的成员变量已损坏
- 如何在c++中为模板函数实例创建快捷方式
- 使用C++中的模板和运算符重载执行矩阵运算
- 有人能分解一下这个c++模板的语法吗
- 如何在c++17中制作一个模板包装器/装饰器
- 模板化建造师专业化
- 调用专用模板时出错"no matching function for call to [...]"
- 模板元程序查找相似的连续类型名称
- 如何在C++20中创建模板别名的推导指南
- 没有名称的C++模板参数
- 具有重复类型的C++可变模板
- 如何将enable-if与模板参数和参数包一起使用
- 没有用于初始化C++中的变量模板的匹配构造函数