2012-01-29 36 views
0

我想在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文件末尾)

+0

的可能重复[实施和模板类的声明为什么要在同头文件?](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

+0

尝试使'isEmpty '内联或静态。 – 2012-01-29 18:03:08

+0

@Nosrettap,文件名是什么?它是'list.cc'还是'dlist.cc'?你需要非常清楚你的问题是什么实际的文件名。 'list.h'或'dlist.h'? – 2012-01-29 18:07:45

回答

3

您需要使用实现更改文件的扩展名。

编译器会处理这个文件进行编译,并且会处理它两次,因为你将它包含在头文件中。

你的文件是这样的:

#include <iostream> 
#include "list.h" 

using namespace std; 

template <class T> 
bool List<T>::isEmpty() 
{ 
    return (firstNode == NULL); 
} 

这将成为

#include <iostream> 
#ifndef __DLIST_H__ 
#define __DLIST_H__ 

template <typename T> 
class List { 

public: 

    bool isEmpty(); 

private: 
    struct node { 
    node *following; 
    node *previous; 
    T  *contents; 
    }; 

    node *firstNode; 
}; 

#include "dlist.cc" 

#endif /* __DLIST_H__ */ 

using namespace std; 

template <class T> 
bool List<T>::isEmpty() 
{ 
    return (firstNode == NULL); 
} 

这将反过来成为

#include <iostream> 
#ifndef __DLIST_H__ 
#define __DLIST_H__ 

template <typename T> 
class List { 

public: 

    bool isEmpty(); 

private: 
    struct node { 
    node *following; 
    node *previous; 
    T  *contents; 
    }; 

    node *firstNode; 
}; 

template <class T> 
bool List<T>::isEmpty() 
{ 
    return (firstNode == NULL); 
} 

#endif /* __DLIST_H__ */ 

using namespace std; 

template <class T> 
bool List<T>::isEmpty() 
{ 
    return (firstNode == NULL); 
} 

因此函数isEmpty()被定义了两次。

将文件重命名为dlist.impl

+0

我明白了。我需要告诉eclipse排除列表。cc,因为它隐式包含在.h文件中。我仍然给你最好的答案,虽然 – Nosrettap 2012-01-29 18:45:03

+0

@Nosrettap这其实是我指出的原因。另外,你应该也可以重命名文件。常见的扩展名是'.impl',你应该坚持这一点。 – 2012-01-29 18:57:01

+0

那么.cc用于那么什么? – Nosrettap 2012-01-29 20:42:54

0

尝试将List<T>::isEmpty()的定义放入与声明类相同的文件中。

+0

这确实解决了问题,但没有解决原因。看看我的答案。 – 2012-01-29 17:54:06

+0

你是对的,我应该提供一个更为详细的理由来说明为什么会发生。我也赞成你。 – josephthomas 2012-01-29 19:40:39

0

鉴于您提供的标题的不寻常形式,为了测试它,您将需要另一个源文件。要开始新的源文件(比如test.cpp),只需要#include "list.h",它将检查任何语法错误,但不会实例化您的List模板。

(只是编译TEST.CPP,不list.cc,因为list.cc间接地TEST.CPP含税)