【发布时间】:2015-03-17 22:32:48
【问题描述】:
我目前正在练习我书中的一些链表作业,但我遇到了编译错误。实现文件似乎很好,但是头文件是接收错误的内容。
这是我的头文件:
#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 ';'
【问题讨论】:
-
你搞砸了你的答案,现在你有错误的代码是不存在的。您应该扩展问题而不是更改,发布另一个问题或使用比 SO 更好的地方来获得此类帮助。
-
可能问题在于
node *linkedList = NULL;,这在 C++03 中是不合法的。省略 `=NULL` 的东西,它既不必要又非法。类中的所有内容都是使用默认构造函数构造的(除非另有明确说明),并且指针初始化为 0 (NULL)。 -
您必须将问题恢复为原始形式。你可以找到它here,这样你就不用再写了。如果您不自己解决,我将不得不标记您的问题以引起版主的注意。
标签: c++ templates linked-list nodes