0
votes

I'm trying to compile the Microsoft Sample "Overloading the << Operator for Your Own Classes" but get the following link error:

error LNK1169: one or more multiply defined symbols found

error LNK2005: "class std::basic_ostream > & __cdecl operator<<(class std::basic_ostream > &,class Date const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABVDate@@@Z) already defined in Date.obj

Anybody any idea why this is not compiling?

2
Do you have a link to the source code? - Peter Alexander
Could you post the code that causes this? - hmjd
There is nothing that may generate this error message. Sample is here: msdn.microsoft.com/en-us/library/1z2f6c2k.aspx. It compiles and runs. - SChepurin
Have you two .cpp files: a main.cpp and Date.cpp? The linker mentions Date.obj, but the example consists of a single source file. If you have made any changes can you post them? - hmjd
The Microsoft sample didn't suggest a file named "Date.cpp". Watch out for code in header files. - Hans Passant

2 Answers

3
votes

If you have the definition of the operator inside a header file, you have to declare it inline, otherwise it will be defined in all translation units that include that header.

But it's probably better to move it to a implementation file, unless you have strong reasons for having it in a header.

0
votes

For me this error was was related to multiple definitions and i solved it following a recommendation from Microsoft. In project properties=>Linker=>Command Line=> Additional Options text box add a command "/FORCE:MULTIPLE". This solved my problem. (https://msdn.microsoft.com/en-us/library/70abkas3.aspx)