简体   繁体   中英

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

I have written the following code to implement Linked List in C++. But I am getting the Errors on compiling it. I think the problem is with the use of 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 .

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> . You can use just node<T> .
  2. It's better to use new instead of malloc (and delete instead of free ).

The class template can be updated to:

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

In main ,

node<int> *ll = NULL;

In 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.

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. General rule do not use malloc in C++ code if you can avoid it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM