我是 C++ 的初学者,所以如果我下面的错误被证明是愚蠢的,请原谅我。尽管如此,我仍然坚持我的代码,并希望得到任何帮助。
尝试通过 g++ 编译时出现以下错误:
In file included from A.cpp:2:
List.h:20: error: ‘List’ is not a template type
A.cpp: In member function ‘void A::NowyObiekt(int)’:
A.cpp:6: error: ‘list_a’ was not declared in this scope
make: *** [A.o] Error 1
我的代码分为以下小文件:
- 啊: http: //pastebin.com/QQ04xx2j(标题)
A.cpp:下面
#include "A.h" #include "List.h" void A::NewObject(int i) { list_a.Add(i); } int A::Compare(int a, int b) { if ( a>b ) return 1; if ( a<b ) return -1; else return 0; }
List.h:下面(标题)
#ifndef LIST_H #define LIST_H template<typename T> class Node { Node() { nxt = pre = 0; } Node(const T& el, Node *n = 0, Node *p = 0 ) { dana = el; nxt = n; pre = p; } T dana; Node *nxt, *pre; }; template<typename T> class List { public: List() { head = tail = 0; } void Add(const T&); protected: Node<T> *head,*tail; }; #endif
List.cpp:http ://pastebin.com/a3HQ9yZ4
prog.cpp:下面(主要)
#include "List.h" #include "A.h" int main() { int i = 5; class List list_a; class A obj; obj.Add(i); }
并且生成文件是: http: //pastebin.com/GTR5jW54
如前所述,我仍然是初学者,所以请理解。我会感谢任何帮助和明确的解释。提前致谢。