gpt4 book ai didi

c++ - 制作模板类后不是类、命名空间、枚举?

转载 作者:可可西里 更新时间:2023-11-01 18:17:03 26 4
gpt4 key购买 nike

我正在尝试对一些类(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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com