multiple definitions error in c++ and solution to solve this issue

) You solve this by ‘defining the class’ in only one cpp file. Why would you want to define it in two files?

2) Don’t define things in header files, only declare them. There are exceptions to this rule, for instance inline functions. Also classes themselves can defined more than once (by this I mean declaring the methods and data members of the class, (i.e. writing class XYZ { ... };) but each definition must be identical. The simplest way to achieve this is to define a class once in a header file. Then each definition resulting from including that header file in multiple places will necessarily be identical.

3) This one is even sillier, it’s one thing to define somethiing twice, but define it twice and differently each time makes even less sense.

I guess the issue is why you think it might sometimes be necessary to define things more than once. Don’t do that.

You should also make it clear what you mean by ‘define the class’. I’ve taken that to mean define the methods and static members of the class. But if you have something else in mind that might be a source of confusion. As usual the best way to avoid this kind of terminology confusion is to post some code.

Leave a Comment