2012-04-16 38 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的目標文件不同的目錄中。因此,鏈接程序找不到它。

當然,這取決於您如何組織模板代碼。