简体   繁体   English

“列表”不是模板

[英]`List' is not a template

Does anyone have any insight in to how this error occurs?有没有人了解这个错误是如何发生的? I made a LinkedList a template in C++ and in my main method I have this code:我在 C++ 中创建了一个 LinkedList 模板,在我的主要方法中,我有以下代码:

List<int> list;
list.insert(1, 9);

And I am getting this error on the first line:我在第一行收到此错误:

`List' is not a template

I am including this file:我包括这个文件:

template <typename T>
class List
{
public:
       List();
       List(const List& aList);
       ~List();

       bool isEmpty() const;
       int getLength() const;
       void insert(int index, const T& newItem);
       void remove(int index);
       void retrieve(int index, T& dataItem) const;

private:
        struct ListNode
        {
               T item;
               ListNode *next;
        };

        int size;
        ListNode *head;

        ListNode *find(int index) const;
};

Not posting the implementation file for spacial reasons but I am post individual functions if necessary.出于空间原因不发布实现文件,但如有必要,我会发布个别功能。

I tried changing List(const List& aList);我尝试更改List(const List& aList); to List(const List<T>& aList);List(const List<T>& aList); but that didn't really change anything.但这并没有真正改变任何东西。 Templating syntax confuses me >.<模板语法让我困惑>.<

In my projects I declare list like this to avoid the error在我的项目中,我像这样声明列表以避免错误

    using namespace std;
    #include <list>

Are you sure that your List template class is not in a different namespace?您确定您的 List 模板类不在不同的命名空间中吗?

Have you tried renaming your template class to something unique to ensure you are instantiating the class you think you are?您是否尝试将模板类重命名为独特的名称以确保实例化您认为的类?

Is it possible that you wrote the template code in the .cpp file?有没有可能你在.cpp文件中写了模板代码?

I'm going of by this line:我要走这条线:

Not posting the implementation file

Template code can only ever be in the .h file, there can be no "implementation file".模板代码只能在 .h 文件中,不能有“实现文件”。
The reasons are as complicated, as they are silly :)原因很复杂,因为它们很愚蠢:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM