我目前正在从我的书中执行一些链接列表分配,并且我没有编译错误。实现文件似乎没问题,但是头文件正在接收错误。
这是我的头文件:
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
#include <string>
using namespace std;
template<class T>
class linkedList {
public:
linkedList();
linkedList(const linkedList& copy);
~linkedList();
int getSize() const;
void addEntry(T entry);
bool deleteEntry(T entry);
T getEntry(int input) const;
linkedList operator=(const linkedList& right);
private:
struct node {
T data;
node<T> *next;
};
node<T> *linkedList = NULL;
};
#include "linkedlist.cpp"
#endif // LINKEDLIST_H
我从编译器获得的错误如下:
c:\users\andym_000\documents\linkedlist\linkedlist.h(22) : error C2059: syntax error : '<'
c:\users\andym_000\documents\linkedlist\linkedlist.h(20) : see reference to class template instantiation 'linkedList<T>::node' being compiled
c:\users\andym_000\documents\linkedlist\linkedlist.h(25) : see reference to class template instantiation 'linkedList<T>' being compiled
c:\users\andym_000\documents\linkedlist\linkedlist.h(22) : error C2238: unexpected token(s) preceding ';'
c:\users\andym_000\documents\linkedlist\linkedlist.h(24) : error C2059: syntax error : '<'
c:\users\andym_000\documents\linkedlist\linkedlist.h(24) : error C2238: unexpected token(s) preceding ';'
c:\users\andym_000\documents\linkedlist\linkedList.h(24) : error C2059: syntax error : '<'
..\linkedList\main.cpp(9) : see reference to class template instantiation 'linkedList<T>' being compiled
with
[
T=std::string
]
c:\users\andym_000\documents\linkedlist\linkedList.h(24) : error C2238: unexpected token(s) preceding ';'
c:\users\andym_000\documents\linkedlist\linkedList.h(24) : error C2059: syntax error : '<'
..\linkedList\main.cpp(10) : see reference to class template instantiation 'linkedList<T>' being compiled
with
[
T=int
]
c:\users\andym_000\documents\linkedlist\linkedList.h(24) : error C2238: unexpected token(s) preceding ';'
答案 0 :(得分:0)
嗯,struct node {...}
不是模板。您的错误告诉您不知道如何处理node<T>
。
答案 1 :(得分:0)
您使用带有node
类的模板表示法,而不是模板。 T
类型是为整个linkedList
定义的 - 在node
的定义中,它是明确定义的类型。
请注意node
在linkedList
内定义,因此linkedList<T>::node
的合格名称为linkedList<T>
(即linkedList
的每个实例化拥有它自己的node
类。
所以,只需将node<T>
替换为node
即可。