Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 模板化C++;错误_C++_Templates_Linked List - Fatal编程技术网

C++ 模板化C++;错误

C++ 模板化C++;错误,c++,templates,linked-list,C++,Templates,Linked List,我正在编写与linkedlist相关的代码,我想对整个程序进行模板化。以下是我写的: template<typename T> class Node { public: T data; Node* next; Node(){}; }; class List{ public:Node<T>* head; List() { head= NULL; } //constructor 模板 类节点 { 公众: T数据; 节点*下一步; Node(){};

我正在编写与linkedlist相关的代码,我想对整个程序进行模板化。以下是我写的:

template<typename T>
class Node
{
public:
    T data;
    Node* next;
    Node(){};
};
class List{
public:Node<T>* head;
List() { head= NULL; } //constructor 
模板
类节点
{
公众:
T数据;
节点*下一步;
Node(){};
};
班级名单{
公共:节点*头;
List(){head=NULL;}//构造函数
因此,它可以与我的其他函数配合使用。不过,我也尝试编写一个函数副本,将列表复制到另一个函数

List Copy(List copyme){
    List<T> x; 
    x = new List<T>;
    Node<T>* current = copyme.head;
    while (current != NULL){
        x.ListInsertHead(current->data);
        current = current->next;
    }
    x.ListReverse();
    return x;
    };
列表副本(列表副本){
清单十;
x=新列表;
节点*当前=copyme.head;
while(当前!=NULL){
x、 ListInsertHead(当前->数据);
当前=当前->下一步;
}
x、 ListReverse();
返回x;
};
我收到了关于模板化类的错误,在这种情况下我应该写什么?谢谢。这些错误只是未声明的标识符,这是因为我模板化错误造成的。

尝试以下方法:

template<typename T>
class Node
{
    public:
       T data;
       Node* next;
       Node(T val){ data = val; }
};

template <typename T>
class List
{
public:
       Node<T>* head;
       List() { head= NULL; } //constructor
};

template <typename T> 
List <T> Copy(List <T> copyme)
{
    List<T> x; 
    x = new List<T>;
    Node<T>* current = copyme.head;
    while (current != NULL){
        x.ListInsertHead(current->data);
        current = current->next;
    }
    x.ListReverse();
    return x;
}
模板
类节点
{
公众:
T数据;
节点*下一步;
节点(T val){data=val;}
};
模板
班级名单
{
公众:
节点*头;
List(){head=NULL;}//构造函数
};
模板
列表副本(列表副本)
{
清单十;
x=新列表;
节点*当前=copyme.head;
while(当前!=NULL){
x、 ListInsertHead(当前->数据);
当前=当前->下一步;
}
x、 ListReverse();
返回x;
}

您收到了哪些错误?请不要只告诉我们“错误”并且省略细节。如果是一堵巨大的错误墙,请向我们显示第一个完整的错误。我们可以帮助您理解它所说的内容。但前提是我们可以看到它。
List
需要是一个带有参数
T
的类模板。我可以将输入参数更改为
Copy
List const©me