c++ - 使用类模板需要模板参数列表链接列表

标签 c++ templates

我正在尝试获取链表,这是我的代码
node.h

template<class Node_entry>
struct Node{
    Node_entry entry;
    Node<Node_entry> *next;

    Node();
    Node(Node_entry entry,Node<Node_entry>* add_on=NULL);
};

template<class Node_entry>
Node<Node_entry>::Node()
{
    next=NULL;
}

template<class Node_entry>
Node<Node_entry>::Node(Node_entry item,Node<Node_entry>* add_on)
{
    entry=item;
    next=add_on;
}

Queue.h

#include "node.h"

enum Error_code{
    success,overflow,underflow
};

template<class Queue_entry>
class Queue {
public:
    Queue();
    bool empty() const;
    Error_code append(const Queue_entry &item);
    Error_code serve();
    Error_code retrieve(Queue_entry &item)const;
    int size()const;
    //Safety features for linked structures
    ~Queue();
    Queue(const Queue<Queue_entry> &original);
    void operator = (const Queue<Queue_entry> &original);
protected:
    Node<Queue_entry> *front, *rear;
};

然后我在下面的代码中遇到了问题:

template<class Queue_entry>
Error_code Queue<Queue_entry>::append(const Queue_entry &item)
{
    Node<Queue_entry> *new_rear=new Node(item);
    if(new_rear==NULL) return overflow;
    if(rear==NULL) front=rear=new_rear;
    else{
        rear->next=new_rear;
        rear=new_rear;
    }
    return success;
}

编译器结果 代码:

Node<Queue_entry> *new_rear=new Node(item);

错误 C2955:“节点”:类模板的使用需要模板参数列表

最佳答案

您在第二次使用 Node 时忘记了模板参数。有问题的行应该是

Node<Queue_entry> *new_rear=new Node<Queue_entry>(item);

关于c++ - 使用类模板需要模板参数列表链接列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20824970/

相关文章:

c++ - GNU 使 ifeq 比较不起作用

c++ - 返回前后字母的程序

c++ - 约束成员模板的外定义规则是什么?

C++构造函数中的通用引用和返回值优化(rvo)

c++ - OpenCV inRange 改变 Mat 类型

c++ - C++/CLR 类中的成员 CComPtr

c++ - 大数组大小 C++ 的问题

c++ - 如何在模板类中使用模板

c++ - 移除模板参数包的最后一个类型

c++ - 为什么我不能用 C++ 中的模板版本覆盖默认的复制构造函数和赋值运算符