Help! Linker error.

Started by
2 comments, last by Ziel 13 years, 9 months ago
So in my program I have four .cpp files and a header file, Global.h, that includes all header files and function prototypes etc. I include Global.h in the four .cpp files, but I get an error saying some constructors are already defined. I don't get why because I use #ifndef #define tags in my header files. Here is the actual error:

1>------ Build started: Project: MTHPH, Configuration: Debug Win32 ------1>Compiling...1>Loop.cpp1>Generating Code...1>Compiling...1>Display.cpp1>FindAvgChance.cpp1>FindHand.cpp1>Generating Code...1>Linking...1>FindAvgChance.obj : error LNK2005: "public: __thiscall Card::Card(void)" (??0Card@@QAE@XZ) already defined in Display.obj1>FindAvgChance.obj : error LNK2005: "public: __thiscall GameInfo::GameInfo(void)" (??0GameInfo@@QAE@XZ) already defined in Display.obj1>FindHand.obj : error LNK2005: "public: __thiscall Card::Card(void)" (??0Card@@QAE@XZ) already defined in Display.obj1>FindHand.obj : error LNK2005: "public: __thiscall GameInfo::GameInfo(void)" (??0GameInfo@@QAE@XZ) already defined in Display.obj1>Loop.obj : error LNK2005: "public: __thiscall Card::Card(void)" (??0Card@@QAE@XZ) already defined in Display.obj1>Loop.obj : error LNK2005: "public: __thiscall GameInfo::GameInfo(void)" (??0GameInfo@@QAE@XZ) already defined in Display.obj1>C:\Users\~~~~\Documents\MTHPH\Debug\MTHPH.exe : fatal error LNK1169: one or more multiply defined symbols found


Help me please!!!
Advertisement
By not submitting a short relevant code snippet, you give people even less information than you have, and you expect that at the same time they should be able to give a more informed analysis than you currently possess. Forum threads shouldn't be a game, let's not make it harder for no reason. ;)

Now, I'll go ahead and try the guessing game. I guess you did the inclusions correctly, and I guess the inclusion chains are ok. I guess that you do not have a file Card.cpp or GameInfo.cpp, and I guess you define non-inlined functions in the Global.h header file. That is, my guess is that your Global.h contains something like following:

#ifndef Global_h#define Global_hclass Card{public:   Card();};Card::Card() // This should not be here. Either inline, move inside the class, or in a separate file Card.cpp (preferred).{}


Was that even close? If not, I'm sure that someone can give you a more helpful answer after you post a short snippet of the actual code.
Yeah, your class function definitions are* in your .h file. Those must not be defined in multiple files and by including those they are being copied to each object (cpp) file.

This means you have a copy of the function in many files how would the linker know which one to use?


* Probably that is.
Thanks for the help guys. I didn't mean to offend by leaving out info. I vaguely remember seeing this topic discussed awhile ago so I thought it was a common problem. I tried searching the forums first but didn't come up with anything. Anyway, once again, thanks.

This topic is closed to new replies.

Advertisement