C ++ LinkList和节点模板链接错误

时间:2016-11-17 17:11:37

标签: c++

我正在尝试创建一个与我的链表模板一起使用的节点模板,但是我收到的错误是Node.h中的构造函数没有定义。我有一个Node.h文件和一个我在Visual Studio中创建的Node.tem文件。 Node.h文件如下所示:

#ifndef NODE_H
#define NODE_H

#include <cstdlib>

template <class Type>
class Node
{
public:
    Node();
    Node(Type indata);

    Type data;
    Node<Type>* next;
    Node<Type>* prev;

};
#include "Node.tem"
#endif

我的Node.tem文件如下所示:

template <class Type>
Node<Type>::Node()
{
    next = nullptr;
}

template <class Type>
Node<Type>::Node(Type indata)
{
    data = indata;
    next = nullptr;
}

经过一些调试后,在这段代码的链接列表模板中我的alloc函数中出现了问题:

template <class Type>
Node<Type>* LinkList<Type>::alloc(Type indata)
{
    Node<Type>* dynamicNode = new Node(indata); //error occurs here
    return dynamicNode;
}

我得到的错误是:

'Node': class has no constructors'Node': use of class template requires template argument list

我的main()代码相当大,因为这只是一个大项目的一小部分,但如果需要我可以发布。

1 个答案:

答案 0 :(得分:2)

我想你忘记了<Type>中的new Node

Node<Type>* dynamicNode = new Node<Type>(indata); 

另外,最好使用auto

auto dynamicNode = new Node<Type>(indata);