2012-04-16 65 views
0

我有以下文件:C++头和源目录

listDriverTest.cpp 
src/List.cpp 
headers/List.h 

在List.cpp的包括是

#include "../headers/List.h" 

在listDriverTest.cpp的包括被

#include "headers/List.h" 

当我用以下语句编译,

g++ listDriverTest.cpp "src/List.cpp" 

我最终得到了相当数量的“未定义参考”错误,例如,

listDriverTest.cpp:(.text+0x81): undefined reference to `List<int>::List()' 
listDriverTest.cpp:(.text+0x8f): undefined reference to `List<int>::add(int)' 
listDriverTest.cpp:(.text+0x9d): undefined reference to `List<int>::add(int)' 
... 

我应该如何正确使用include和编译这三个文件以便编译正常工作?我已经得到listDriverTest.cpp编译和正确运行在同一目录中的所有文件,但不是当他们这样分手时。

+0

我很惊讶你没有得到一个非常具体的错误无法找到list.h头。 – 2012-04-16 17:47:37

+1

您需要在使用模板时定义头文件中的所有内容http://www.parashift.com/c++-faq-lite/templates.html#faq-35.13 – 2012-04-16 17:47:55

+0

*未定义的引用*是链接器错误,因此它很少与您的标题/来源位置无关。 – 2012-04-16 18:09:09

回答

0

您的程序在我的机器上正确编译。
只需删除src/List.cpp中的双引号 我认为你的问题是别的。

我在list.cpp中添加了一个函数void list(void),其中打印了“list”。
相同的签名被添加到list.h中。

1

它看起来像通过编译src/List.cpp生成的目标文件已经包含专门化列表,但它在与listDriversTest.cpp的目标文件不同的目录中。因此,链接程序找不到它。

当然,这取决于您如何组织模板代码。