类实例化w /模板类出错

时间:2012-12-10 17:44:24

标签: c++ class templates instantiation doubly-linked-list

我无法弄清楚我的程序出了什么问题。我用头文件中的类声明和成员函数声明,一个.cpp文件中的成员函数定义构建它,然后我在main.cpp中有一个主驱动程序。我把它们全部放在ideone上的一个文件中,以便我可以在这里发布程序:

http://ideone.com/PPZMuJ

ideone上显示的错误确实是我的IDE在构建时显示的错误。有人可以指出我做错了吗?

错误是:

prog.cpp: In instantiation of ‘IntDLLNode<int>’:
prog.cpp:56:   instantiated from ‘void IntDLList<T>::addToDLLHead(const T&) [with T = int]’
prog.cpp:215:   instantiated from here
prog.cpp:8: error: template argument required for ‘struct IntDLList’

Line 56: head = new IntDLLNode<T>(el,head,NULL);
Line 215: dll.addToDLLHead(numero);
Line 8: class IntDLLNode    {

你可以忽略try / catch子句,我还没有完成那部分的工作 - 我只是试图超越当前的错误。

1 个答案:

答案 0 :(得分:0)

问题出在朋友声明中:

template <class T>
class IntDLLNode    {
  friend class IntDLList;
  // rest of IntDLLNode here
};

在此声明非模板class IntDLList为朋友。稍后,您声明一个具有相同名称的模板类;但与你期望的不同,它不会成为IntDLLNode的朋友。

要解决此问题,请指定朋友类是模板:

template <class U> class IntDLList;

template <class T>
class IntDLLNode    {
  friend class IntDLList<T>;
  // rest of IntDLLNode here
};
相关问题