简体   繁体   English

为什么我遇到错误,“结构节点”需要模板参数?

[英]Why I am getting error, template argument required for ‘struct node’?

I have written the following code to implement Linked List in C++. 我编写了以下代码以在C ++中实现链接列表。 But I am getting the Errors on compiling it. 但是我在编译它时遇到了错误。 I think the problem is with the use of Template. 我认为问题在于使用Template。

#include<iostream>
template<class T>
struct node{
    T data;
    struct node *next;
};

template<class T>
void push(struct node **headRef ,T data){
    struct node *temp =(struct node*) malloc(sizeof(struct node));
    temp->data=data;
    temp->next=*headRef;
    *headRef=temp;
}
int main(){
    struct node *ll = NULL;
    push(&ll,10);
    push(&ll,3);
    push(&ll,6);
    push(&ll,8);
    push(&ll,13);
    push(&ll,12);
}

Error 错误

MsLL.cpp:9:18: error: template argument required for ‘struct node’
MsLL.cpp: In function ‘void push(int**, T)’:
MsLL.cpp:10:8: error: template argument required for ‘struct node’
MsLL.cpp:10:19: error: invalid type in declaration before ‘=’ token
MsLL.cpp:10:28: error: template argument required for ‘struct node’
MsLL.cpp:10:28: error: template argument required for ‘struct node’
MsLL.cpp:10:21: error: expected primary-expression before ‘struct’
MsLL.cpp:10:21: error: expected ‘)’ before ‘struct’
MsLL.cpp:11:7: error: request for member ‘data’ in ‘temp->’, which is of non-class type ‘int’
MsLL.cpp:12:7: error: request for member ‘next’ in ‘temp->’, which is of non-class type ‘int’
MsLL.cpp: In function ‘int main()’:
MsLL.cpp:16:8: error: template argument required for ‘struct node’
MsLL.cpp:16:17: error: invalid type in declaration before ‘=’ token
struct node *ll = NULL;

is not valid. 无效。 You have to use a template instantiation syntax. 您必须使用模板实例化语法。

struct node<int> *ll = NULL;

Similarly, you have to use the template parameter in push . 同样,您必须在push使用template参数。

template<class T>
void push(struct node<T> **headRef ,T data){
    struct node<T> *temp =(struct node<T>*) malloc(sizeof(struct node<T>));

Suggestions for general improvement 一般改善建议

  1. You don't need to use struct node<T> . 您不需要使用struct node<T> You can use just node<T> . 您可以只使用node<T>
  2. It's better to use new instead of malloc (and delete instead of free ). 最好使用new而不是malloc (并使用delete代替free )。

The class template can be updated to: 该类模板可以更新为:

template<class T>
struct node{
    T data;
    node *next;
};

In main , main

node<int> *ll = NULL;

In push : push

template<class T>
void push(node<T> **headRef ,T data){
    node<T> *temp = new node<T>();

In all places you write node you need to add the template arg. 在所有写入节点的地方都需要添加模板arg。

template<class T>
struct node 
{
    T data;
    struct node *next;
};

template<class T>
void push(struct node<T> **headRef ,T data){
    struct node<T> *temp =(struct node<T>*) malloc(sizeof(struct node<T>));
    temp->data=data;
    temp->next=*headRef;
    *headRef=temp;
}

You should also use new instead of malloc to make sure the constructor runs on the object, using malloc can cause you grief because it just allocates a chunk of memory and does not know anything about constructors. 您还应该使用new而不是malloc来确保构造函数在对象上运行,使用malloc可能会让您感到悲伤,因为malloc只是分配了一块内存,并且对构造函数一无所知。 General rule do not use malloc in C++ code if you can avoid it. 如果可以避免,一般规则不要在C ++代码中使用malloc。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误:'struct List'需要模板参数 - error: template argument required for 'struct List' 为什么会出现此模板编译错误? - Why am i getting this template compile error? 为什么在使用花括号初始化结构时出现错误? - Why am I getting error while initializing a struct with curly braces? 为什么我收到错误“(指针名称)不是模板)? - why i am getting error " (name of the pointer ) is not a template )? 为什么我的模板函数指针出现链接器错误? - Why am I getting a Linker error with template function pointer? 为什么我没有获得所需程序的输出? - why i am not getting the output for the required program? 我收到“模板参数推导/替换失败:”C++ 中的错误 - I am getting " template argument deduction/substitution failed:" Error in C++ 为什么我得到这个错误,&#39;method&#39;的左边必须有class / struct / union? - Why am I getting this error, left of 'method' must have class/struct/union? 为什么我收到错误“非模板&#39;f&#39;用作模板” - Why am I getting the error “non-template 'f' used as template” 我正在编码Node结构,我认为这是构造函数初始化程序错误 - I am coding Node struct and I think this is constructor initializer error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM