我有一个奇怪的错误,说我的类正在重新定义,源于我的 Node 类中的 friend 声明。这是我当前的代码:
template <class T>
class Node {
private:
T val;
Node<T> * next;
public:
friend class OList;
};
任何我的其他类(class):
template <class T>
class OList { ------> Error here
private:
Node<T> * head;
int size;
public:
OList();
OList(const OList<T> &b);
~OList();
clear();
int size();
T tGet(int input);
int count(T input);
insert (T input);
remove (T input);
uniquify(T input);
Node<T> * returnHead();
};
// Constructs empty list
template <class T>
OList<T>::OList() { ---> Error here
head = NULL;
size = 0;
}
最佳答案
OList
不是一个类,它是一个类模板。您可以将模板的所有特化加为好友:
template <typename> friend class OList;
或成为特定专业的 friend :
friend class OList<T>;
这将要求 OList
已经声明。在 Node
的定义之前放置一个前向声明:
template <typename> class OList;
关于c++ - 奇怪的类重新定义错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26898186/