【问题标题】:use of class template requires template argument list link list使用类模板需要模板参数列表链接列表
【发布时间】:2013-12-29 12:02:30
【问题描述】:

我正在尝试获取链接列表,这是我的代码
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:“节点”:使用类模板需要模板参数列表

【问题讨论】:

  • 听起来不言自明。它告诉您忘记提供模板参数列表(即&lt;...&gt; 形式的东西)。
  • 谢谢!你能告诉我如何更改我的代码以使其可运行吗?我在东这个有一些问题。

标签: c++ templates


【解决方案1】:

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

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

【讨论】:

  • 谢谢!这就是问题所在。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多