incomplet type and forward declaration
编译一下代码时会出如标题的错误,请问是怎么回事,怎样改?
1 #include <stdlib.h>
3 using namespace std;
6 template <typename T>
7 struct Lnode
8 {
9 T data;
10 struct Lnode* next;
11
12 };
13
21 template <class T>
22 class LinkList
23 {
24
25 public:
26 LinkList();
27 ~LinkList();
28 void creatList();
29 bool insert(T,int);
34 bool deleteNode(T);
bool locate(T,int &); // return the position of the ith element
36 bool retrive(T&, int ); // return the ith element
37
38
39 private:
40 struct Lnode<T>* head;
41
42 };//class
43
44 LinkList<class T>::LinkList()
45 {
46 }
47
48 LinkList<class T>::~LinkList()
49 {
50
51 }
52
53 void LinkList<class T>::creatList()
54 {
55 //head=new Lnode<T>;
56 //head->next=NULL;
57 Lnode<T>* p=new Lnode<T>;
58 //head=p;
59
60
61 }
62 int main()
63 {
64 //LinkList<int> L;
65 Lnode<int> L;
66
67 return 1;
68 }
错误信息为:
linklist.cpp: In instantiation of `Lnode<T>':
linklist.cpp:57: instantiated from here
linklist.cpp:9: error: `Lnode<T>::data' has incomplete type
linklist.cpp:44: error: forward declaration of `struct T'
makefile:2: recipe for target `x' failed
make: *** [x] Error 1
[解决办法]
第53行,把
void LinkList<class T>::creatList()
改成
template <class T>
void LinkList<T>::creatList()
[解决办法]
我编译了下你的代码,出现的错误更多,不只是你说的这些。不知道你的开发环境是怎样的?
修改了下,编译通过:
#include <stdlib.h>using namespace std;template <typename T>struct Lnode{ T data; struct Lnode* next;};template <typename T>class LinkList{ public: LinkList(); ~LinkList(); void creatList(); bool insert(T,int); bool deleteNode(T); bool locate(T,int &); // return the position of the ith element bool retrive(T&, int ); // return the ith element private: struct Lnode<T>* head;};//classtemplate <typename T>LinkList<T>::LinkList(){}template <typename T>LinkList<T>::~LinkList(){}template <typename T>void LinkList<T>::creatList(){ //head=new Lnode<T>; //head->next=NULL; Lnode<T>* p=new Lnode<T>; //head=p;}int main(){ //LinkList<int> L; Lnode<int> L; return 1;}