“结构节点”所需的模板参数出错的原因? 我编写了以下代码来实现C++中的链表。但是我在编译它时发现了错误。我认为问题在于模板的使用 #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); }
无效。您必须使用模板实例化语法“结构节点”所需的模板参数出错的原因? 我编写了以下代码来实现C++中的链表。但是我在编译它时发现了错误。我认为问题在于模板的使用 #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); },c++,C++,无效。您必须使用模板实例化语法 struct node<int> *ll = NULL; 总体改进建议 您不需要使用结构节点。您可以只使用一个节点。 最好使用new而不是malloc,使用delete而不是free。 类模板可以更新为: template<class T> struct node{ T data; node *next; }; 推送: 无效。您必须使用模板实例化语法 struct node<int> *ll = NULL;
struct node<int> *ll = NULL;
总体改进建议
您不需要使用结构节点。您可以只使用一个节点。
最好使用new而不是malloc,使用delete而不是free。
类模板可以更新为:
template<class T>
struct node{
T data;
node *next;
};
推送:
无效。您必须使用模板实例化语法
struct node<int> *ll = NULL;
总体改进建议
您不需要使用结构节点。您可以只使用一个节点。
最好使用new而不是malloc,使用delete而不是free。
类模板可以更新为:
template<class T>
struct node{
T data;
node *next;
};
推送:
在编写节点的所有位置,都需要添加模板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;
}
您还应该使用new而不是malloc来确保构造函数在对象上运行,使用malloc可能会让您感到悲伤,因为它只分配了一块内存,对构造函数一无所知。一般规则不使用Mulc在C++代码中,如果你能避免它。
在你写节点的所有地方,你需要添加模板ARG.< /P>
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;
}
您还应该使用new而不是malloc来确保构造函数在对象上运行,使用malloc可能会让您感到悲伤,因为它只分配了一块内存,对构造函数一无所知。一般规则不在C++代码中使用MALLC,如果你能避免它, UR节点类型是模板化的,这就是为什么。你可能想用NoDEORE替换错误位置中的节点,是否有一个令人信服的理由使用MALOC而不是新的?只有代码中的模板行可以被认为是C++。剩下的是C。几乎没有理由在C++中写C++类型。@ PraceToRoin然后我将如何改变推函数中的头值?@ PraceToRIN,如果一个符合条件的C++编译器编译它没有错误,它是C++。C风格C++可能是,但是C++允许UR节点类型是模板化的,这就是为什么。你可能想用NoDEORE替换错误位置中的节点,是否有一个令人信服的理由使用MALOC而不是新的?只有代码中的模板行可以被认为是C++。剩下的是C。几乎没有理由在C++中写C++类型。@ PraceToRoin然后我将如何改变推函数中的头值?@ PraceToRIN,如果一个符合条件的C++编译器编译它没有错误,它是C++。C风格的C++,也许C++允许
template<class T>
void push(node<T> **headRef ,T data){
node<T> *temp = new node<T>();
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;
}