2010-12-04 110 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; 
}