我想在Eclipse中创建链接列表类,但我无法正确编译它。如何在Eclipse中编译模板类(C++)?
这里是我的.cc文件(一小段代码片段)
#include <iostream>
#include "list.h"
using namespace std;
template <class T>
bool List<T>::isEmpty()
{
return (firstNode == NULL);
}
,这里是我的list.h文件(一小段代码片段)
#ifndef __LIST_H__
#define __LIST_H__
template <typename T>
class List {
public:
bool isEmpty();
private:
struct node {
node *following;
node *previous;
T *contents;
};
node *firstNode;
};
#include "list.cc"
#endif /* __LIST_H__ */
我尝试 “建设全部” 在日食,但我得到以下错误:
make all
Building file: ../list.cc
Invoking: Cross G++ Compiler
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"list.d" -MT"list.d" -o "list.o" "../list.cc"
../list.cc:13: error: redefinition of 'bool List<T>::isEmpty()'
../list.cc:13: error: 'bool List<T>::isEmpty()' previously declared here
make: *** [list.o] Error 1
请帮助...谢谢。我会很乐意提供任何需要澄清
编辑:我被给了.h文件,所以我知道它是正确的。我也知道我应该有一个名为list.cc的.cc文件(它包含在.h文件末尾)
的可能重复[实施和模板类的声明为什么要在同头文件?](http://stackoverflow.com/questions/3749099/why-should-the-implementation-and-the-declaration-of-a-template-class-be-in-the) – 2012-01-29 17:58:34
尝试使'isEmpty '内联或静态。 – 2012-01-29 18:03:08
@Nosrettap,文件名是什么?它是'list.cc'还是'dlist.cc'?你需要非常清楚你的问题是什么实际的文件名。 'list.h'或'dlist.h'? – 2012-01-29 18:07:45