模板类声明c ++

时间:2013-11-23 22:29:03

标签: c++ class templates declaration

我认为这只是语法问题。但无论我怎么做,我都会遇到编译错误。我正在使用基于节点的列表类,无法弄清楚如何编写声明头。我在哪里放置前向List类声明等?我只是不知道如何设置它。以下是整个声明标题:

#include <iostream>

using namespace std;
class List;

template <typename T>
class Node{
   private:
      Node(T, Node*);
      T data;
      Node* next;
      friend class List<T>;
      friend ostream& operator<<(ostream&, const List<T>&);
};

class List{
   public:
      List(int = 0);
      List(const List&);
      ~List();
      bool gotoBeginning();
      bool gotoEnd();
      bool gotoNext();
      bool gotoPrior();
      bool insertAfter(T);
      bool insertBefore(T);
      bool remove(T&);
      bool replace(T);
      bool getCursor(T&) const;
      bool empty() const;
      bool full() const;
      bool clear();
      List<T>& operator=(const List&);
      friend ostream& operator<<(ostream&, const List<T>&);
      bool operator==(const List&) const;
  private:
      Node* head;
      Node* cursor;
};

1 个答案:

答案 0 :(得分:1)

将其更改为

template <class T>
class List 

并将T类型添加到节点声明

Node<T>* head;
Node<T>* cursor;