我有以下文件: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