2012-10-24 81 views
1

可能重複:
What is an undefined reference/unresolved external symbol error and how do I fix it?類未定義參考

這應該是一個簡單的問題,但我仍然有問題。我已經看過類似的問題,但他們似乎有點複雜,然後我正在尋找。

我使用Code :: Blocks 10.05。

我收到了一個未定義的對我的類文件的引用。我知道這是一個鏈接器錯誤,但我不知道如何解決它。我很害怕我對鏈接文件不太瞭解。

s\Desktop\the cool container\Test 1\Class2.o:Class2.cpp|| undefined reference to `Burrito::Burrito()'| 
||=== Build finished: 1 errors, 0 warnings ===| 

我所有的文件都在同一個文件夾中。我的對象什麼都不做,我只是試試這個,我不應該從另一個類創建一個對象。

啊,這是從TheNewBoston教程...他能得到這個工作,所以我應該能夠得到它的工作。

如果我包括在Class2.cpp #include "Burrito.cpp"那麼它的工作原理,但他並沒有包括。


------------ Class2.cpp -----------------

#include <iostream> 
#include "Burrito.h" 
using namespace std; 
int main(){ 

    Burrito Bo; 
    return 0; 
} 

- -------------- Burrito.h ----------------

#ifndef BURRITO_H 
#define BURRITO_H 


class Burrito 
{ 
    public: 
     Burrito(); 

}; 

#endif // BURRITO_H 

---------- ----- Burrito.cpp -----------------

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

using namespace std; 
Burrito::Burrito() 
{ 

} 
+0

最有可能[this](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-doi-i-fix/12574400 #12574400) - 你沒有編譯或鏈接'Burrito.cpp'。 –

+0

請詳細說明你編譯你的文件的方式來生成一個可執行文件... – sergio

+0

我只是想我可以構建它並運行它我沒有意識到我不得不做任何特殊的編譯。我會研究一下。 – user1771489

回答

0

如果我包括在Class2.cpp #include "Burrito.cpp"那麼它的工作原理,但他並沒有包括。

這是很少(我絕不會說)的解決方案。不要#include什麼應該是其他源文件中的源文件。解決方案是編譯所有需要編譯的文件,並讓鏈接器處理鏈接問題。

你沒有編譯Burrito.cpp,所以當然你有聯繫的問題。解決方案很簡單:編譯Burrito.cpp並將Burrito.o添加到您提供給鏈接程序的文件集。或者,您可以將Class2.cpp和Burrito.cpp提供給編譯器,從而創建可執行文件而不是目標文件作爲輸出。

+0

我讀過關於類似的東西,但我如何編譯Burrito.cpp?我想我會在有空的時候再看看。 – user1771489