0

我无法弄清楚我的程序出了什么问题。我用头文件中的类声明和成员函数声明,一个.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 子句,我还没有完成那部分的工作——我只是想克服当前的错误。

4

1 回答 1

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
};
于 2012-12-10T20:50:26.743 回答