2010-12-04 115 views
1

所以我試圖把我的遊戲分成多個文件。我得到這些錯誤:C++鏈接器錯誤

1>item.obj : error LNK2005: "private: static class sf::Image ctile::itile" ([email protected]@@[email protected]@@A) already defined in character.obj 
1>item.obj : error LNK2005: "class cmap maps" ([email protected]@[email protected]@A) already defined in character.obj 
1>item.obj : error LNK2005: "class cmainchar mainch" ([email protected]@[email protected]@A) already defined in character.obj 
1>item.obj : error LNK2005: "class citemmanager itemmanager" ([email protected]@[email protected]@A) already defined in character.obj 
1>item.obj : error LNK2005: "private: static class sf::Image citem::iitem" ([email protected]@@[email protected]@@A) already defined in character.obj 
1>item.obj : error LNK2005: "private: static class sf::Image cspell::ispell" ([email protected]@@[email protected]@@A) already defined in character.obj 
1>item.obj : error LNK2005: "class sf::RenderWindow App" ([email protected]@[email protected]@@A) already defined in character.obj 
1>item.obj : error LNK2005: "class sf::View View" ([email protected]@[email protected]@A) already defined in character.obj 
1>main.obj : error LNK2005: "private: static class sf::Image ctile::itile" ([email protected]@@[email protected]@@A) already defined in character.obj 
1>main.obj : error LNK2005: "class cmap maps" ([email protected]@[email protected]@A) already defined in character.obj 
1>main.obj : error LNK2005: "class cmainchar mainch" ([email protected]@[email protected]@A) already defined in character.obj 
1>main.obj : error LNK2005: "class citemmanager itemmanager" ([email protected]@[email protected]@A) already defined in character.obj 
1>main.obj : error LNK2005: "private: static class sf::Image citem::iitem" ([email protected]@@[email protected]@@A) already defined in character.obj 
1>main.obj : error LNK2005: "private: static class sf::Image cspell::ispell" ([email protected]@@[email protected]@@A) already defined in character.obj 
1>main.obj : error LNK2005: "class sf::RenderWindow App" ([email protected]@[email protected]@@A) already defined in character.obj 
1>main.obj : error LNK2005: "class sf::View View" ([email protected]@[email protected]@A) already defined in character.obj 
1>map.obj : error LNK2005: "private: static class sf::Image ctile::itile" ([email protected]@@[email protected]@@A) already defined in character.obj 
1>map.obj : error LNK2005: "class cmap maps" ([email protected]@[email protected]@A) already defined in character.obj 
1>map.obj : error LNK2005: "class cmainchar mainch" ([email protected]@[email protected]@A) already defined in character.obj 
1>map.obj : error LNK2005: "class citemmanager itemmanager" ([email protected]@[email protected]@A) already defined in character.obj 
1>map.obj : error LNK2005: "private: static class sf::Image citem::iitem" ([email protected]@@[email protected]@@A) already defined in character.obj 
1>map.obj : error LNK2005: "private: static class sf::Image cspell::ispell" ([email protected]@@[email protected]@@A) already defined in character.obj 
1>map.obj : error LNK2005: "class sf::RenderWindow App" ([email protected]@[email protected]@@A) already defined in character.obj 
1>map.obj : error LNK2005: "class sf::View View" ([email protected]@[email protected]@A) already defined in character.obj 
1>spell.obj : error LNK2005: "private: static class sf::Image ctile::itile" ([email protected]@@[email protected]@@A) already defined in character.obj 
1>spell.obj : error LNK2005: "class cmap maps" ([email protected]@[email protected]@A) already defined in character.obj 
1>spell.obj : error LNK2005: "class cmainchar mainch" ([email protected]@[email protected]@A) already defined in character.obj 
1>spell.obj : error LNK2005: "class citemmanager itemmanager" (?it[email protected]@[email protected]@A) already defined in character.obj 
1>spell.obj : error LNK2005: "private: static class sf::Image citem::iitem" ([email protected]@@[email protected]@@A) already defined in character.obj 
1>spell.obj : error LNK2005: "private: static class sf::Image cspell::ispell" ([email protected]@@[email protected]@@A) already defined in character.obj 
1>spell.obj : error LNK2005: "class sf::RenderWindow App" ([email protected]@[email protected]@@A) already defined in character.obj 
1>spell.obj : error LNK2005: "class sf::View View" ([email protected]@[email protected]@A) already defined in character.obj 
+5

與其開始* *您的遊戲*,我建議嘗試採取一個簡單的「hello world」風格的應用程序,並將其分成兩個或多個文件。這樣你可以(a)在這裏發佈代碼作爲例子,並且(b)更容易理解重要的概念。我們無法幫助您僅使用錯誤消息列表來調試代碼。 – 2010-12-04 23:09:25

回答

2

問題是您的類成員函數的定義放在頭文件中。所以,相同的定義將轉換爲單獨的翻譯單位。您可以在頭文件中使用include guards

+1

我真的想downvote你的答案。也許他沒有包括守衛,但錯誤來自鏈接器,所以包括守衛將無法解決它。 – ybungalobill 2010-12-04 23:14:13

+0

@ybungalobill:LNK2005是一個多重定義錯誤。因此,如果OP在同一個翻譯單元中多次包含一個沒有包含保護的頭文件,連接器將會拋出這個錯誤。添加包括警衛將解決這個問題。 – Praetorian 2010-12-04 23:31:09

4

您可能在.h文件中有您的方法定義。其結果是,你有你的

一個定義的多個副本保留的聲明與包括警衛.h文件:

#ifndef SOMETHING_H_ 
#define SOMETHING_H_ 

class Something { 
public: 
    int foo(); 
}; 

#endif // SOMETHING_H_ 

和方法定義在.cpp文件:

#include "Something.h" 

int Something::foo() { 
    return 5; 
}