C++ Linker Errors
So I tried to divide my game into multiple files. I get these errors:
1>item.obj : error LNK2005: "private: static class sf::Image ctile::itile" (?itile@ctile@@0VImage@sf@@A) already defined in character.obj
1>item.obj : error LNK2005: "class cmap maps" (?maps@@3Vcmap@@A) already defined in character.obj
1>item.obj : error LNK2005: "class cmainchar mainch" (?mainch@@3Vcmainchar@@A) already defined in character.obj
1>item.obj : error LNK2005: "class citemmanager itemmanager" (?itemmanager@@3Vcitemmanager@@A) already defined in character.obj
1>item.obj : error LNK2005: "private: static class sf::Image citem::iitem" (?iitem@citem@@0VImage@sf@@A) already defined in character.obj
1>item.obj : error LNK2005: "private: static class sf::Image cspell::ispell" (?ispell@cspell@@0VImage@sf@@A) already defined in character.obj
1>item.obj : error LNK2005: "class sf::RenderWindow App" (?App@@3VRenderWindow@sf@@A) already defined in character.obj
1>item.obj : error LNK2005: "class sf::View View" (?View@@3V0sf@@A) already defined in character.obj
1>main.obj : error LNK2005: "private: static class sf::Image ctile::itile" (?itile@ctile@@0VImage@sf@@A) already defined in character.obj
1>main.obj : error LNK2005: "class cmap maps" (?maps@@3Vcmap@@A) already defined in character.obj
1>main.obj : error LNK2005: "class cmainchar mainch" (?mainch@@3Vcmainchar@@A) already defined in character.obj
1>main.obj : error LNK2005: "class citemmanager itemmanager" (?itemmanager@@3Vcitemmanager@@A) already defined in character.obj
1>main.obj : error LNK2005: "private: static class sf::Image citem::iitem" (?iitem@citem@@0VImage@sf@@A) already defined in character.obj
1>main.obj : error LNK2005: "private: static class sf::Image cspell::ispell" (?ispell@cspell@@0VImage@sf@@A) already defined in character.obj
1>main.obj : error LNK2005: "class sf::RenderWindow App" (?App@@3VRenderWindow@sf@@A) already defined in character.obj
1>main.obj : error LNK2005: "class sf::View View" (?View@@3V0sf@@A) already defined in character.obj
1>map.obj : error LNK2005: "private: static class sf::Image ctile::itile" (?itile@ctile@@0VImage@sf@@A) already defined in character.obj
1>map.obj : error LNK2005: "class cmap maps" (?maps@@3Vcmap@@A) already defined in character.obj
1>map.obj : error LNK2005: "class cmainchar mainch" (?mainch@@3Vcmainchar@@A) already defined in character.obj
1>map.obj : error LNK2005: "class citemmanager itemmanager" (?itemmanager@@3Vcitemmanager@@A) already defined in character.obj
1>map.obj : error LNK2005: "private: static class sf::Image citem::iitem" (?iitem@citem@@0VImage@sf@@A) already defined in character.obj
1>map.obj : error LNK2005: "private: static class sf::Image cspell::ispell" (?ispell@cspell@@0VImage@sf@@A) already defined in character.obj
1>map.obj : error LNK2005: "class sf::RenderWindow App" (?App@@3VRenderWindow@sf@@A) already defined in character.obj
1>map.obj : error LNK2005: "class sf::View View" (?View@@3V0sf@@A) already defined in character.obj
1>spell.obj : error LNK2005: "private: static class sf::Image ctile::itile" (?itile@ctile@@0VImage@sf@@A) already defined in character.obj
1>spell.obj : error LNK2005: "class cmap maps" (?maps@@3Vcmap@@A) already defined in character.obj
1>spell.obj : error LNK2005: "class cmainchar mainch" (?mainch@@3Vcmainchar@@A) already defined in character.obj
1>spell.obj : error LNK2005: "class citemmanager itemmanager" (?itemmanager@@3Vcitemmanager@@A) already defined in character.obj
1>spell.obj :开发者_运维技巧 error LNK2005: "private: static class sf::Image citem::iitem" (?iitem@citem@@0VImage@sf@@A) already defined in character.obj
1>spell.obj : error LNK2005: "private: static class sf::Image cspell::ispell" (?ispell@cspell@@0VImage@sf@@A) already defined in character.obj
1>spell.obj : error LNK2005: "class sf::RenderWindow App" (?App@@3VRenderWindow@sf@@A) already defined in character.obj
1>spell.obj : error LNK2005: "class sf::View View" (?View@@3V0sf@@A) already defined in character.obj
You probably have your method definitions in .h files. As a result, you have many copies of a single definition in your
Keep the declarations in the .h file with include guards:
#ifndef SOMETHING_H_
#define SOMETHING_H_
class Something {
public:
int foo();
};
#endif // SOMETHING_H_
and the method definitions in the .cpp file:
#include "Something.h"
int Something::foo() {
return 5;
}
The problem is that definitions of your class member functions are placed in header files. So, the same definitions go to separate translation units.
Also, use include guards in your header files.
精彩评论