- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在尝试对一些类(LinkedListNode 和 LinkedList)进行模板化,这样
template <class T>
class LinkedListNode{
public:
T data;
LinkedListNode *next;
LinkedListNode();
};
在我的 LinkedList 类中,我有私有(private)变量:
private:
LinkedListNode *head;
//iterator for traversing the list
LinkedListNode *current;
};
编译时出现奇怪的错误:
./LinkedList.h:38:3: error: unknown type name 'LinkedListNode'; did you mean 'LinkedList'? LinkedListNode *head; ^~~~~~~~~~~~~~ LinkedList ./LinkedList.h:13:7: note: 'LinkedList' declared here class LinkedList{ ^ ./LinkedList.h:40:3: error: unknown type name 'LinkedListNode'; did you mean 'LinkedList'? LinkedListNode *current; ^~~~~~~~~~~~~~ LinkedList ./LinkedList.h:13:7: note: 'LinkedList' declared here class LinkedList{ ^ LinkedList.cpp:7:1: error: 'LinkedListNode' is not a class, namespace, or enumeration LinkedListNode::LinkedListNode(){ ^ ./LinkedList.h:5:7: note: 'LinkedListNode' declared here class LinkedListNode{ ^
如果它说我的 LinkedListNode 也被声明,为什么我会收到这些错误?
最佳答案
LinkedListNode
不是类型,而是 LinkedListNode<T>
是。务必implement LinkedListNode::LinkedListNode()
and other member functions in the header file , 和 #include
此头文件在 LinkedList<T>
的定义之前.
template <class T>
class LinkedList
{
private:
LinkedListNode<T> *head;
LinkedListNode<T> *current;
}
关于c++ - 制作模板类后不是类、命名空间、枚举?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35871847/
我是一名优秀的程序员,十分优秀!