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

use of class template requires template argument list link list

本文关键字:列表 链接 参数      更新时间:2023-10-16

我正在尝试获取链表,这是我的代码
节点.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;
}

队列.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);