我有以下文件: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编译和正确运行在同一目录中的所有文件,但不是当他们这样分手时。
我很惊讶你没有得到一个非常具体的错误无法找到list.h头。 – 2012-04-16 17:47:37
您需要在使用模板时定义头文件中的所有内容http://www.parashift.com/c++-faq-lite/templates.html#faq-35.13 – 2012-04-16 17:47:55
*未定义的引用*是链接器错误,因此它很少与您的标题/来源位置无关。 – 2012-04-16 18:09:09