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

标签 c++

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

相关文章:

c++ - 使用自定义转换运算符向上转换到模板类

c++ - 列表中的 std::min_element 不返回最小值

c++ - 保证复制省略和 Nonmoveable{Nonmoveable{}}

对相同类类型的 C++ 引用公开私有(private)成员

c++ - 使用可变参数模板实现 Get(Tuple) 得到 "parameter packs not expanded with ‘...’“错误

c++ - C++ 中的缓冲区大小

c++ - 缺少下标c++

c++ - 在带有变量包含参数的 makefile 中添加 Windows 路径

c++ - 在操作后使用 vector 的原始值

c++ - 如何从文本文件的每一行中获取一个整数?