【问题标题】:Error on class instantiation w/ template classes带有模板类的类实例化错误
【发布时间】:2012-11-28 04:19:09
【问题描述】:

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

【问题讨论】:

  • 你能显示更多的代码吗?这些是不同区域的随机行...需要查看定义和一些代码,一些上下文

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


【解决方案1】:

问题出在朋友声明中:

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

在这里你声明一个非模板class IntDLList 成为朋友。稍后,您声明一个具有相同名称的 template 类;但不像你想象的那样,它不会成为IntDLLNode的朋友。

要解决这个问题,请将朋友类指定为模板:

template <class U> class IntDLList;

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

【讨论】:

  • 谢谢!按预期工作。
猜你喜欢
  • 2019-11-17
  • 2014-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多