I have been working on a program in windows VC++ 2008. I started by having all of my code in .cpp files (just to get everything working), and now breaking things into .h, and .cpp files. when I compile I get a mountain of LNK2005 errors stating that:
Object.obj : error LNK2005: "__thiscall thing::thing(args)" already defined in otherObject.obj
while I was making the original program I kept getting errors of undeclared identifier, and so I gave a include directive to satisfy that. now when I am breaking everything up into separate .cpp, and .h files I get all of this. which place do I start looking (Object, otherObject, or thing), or somewhere else.
Basically you have definition for
thing::thing(args)in two Translation Units(TU), which violates the One Definition Rule(ODR) and hence the error.The linker exactly tells you which TU’s are involved:
otherObject.objandObject.obj.Start looking in to
otherObject.cppandObject.cppand the headers which are included in these twocppfiles. It is most likely that you have defined your constructorthing::thing(args)in header file and including that header file in both thesecppfiles results in multiple definitions.Suggested Solution:
You cannot define the constructor in header file, You need to add it to your
cppfile along with other member functions of the class. If you must add the definition of constructor to header you should mark itinline, given that You have not shown your code I don’t see any reason to apply the second approach.