Microsoft Overloading the << Operator Sample throws link errors - c++

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?

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.

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)

Related

Linker error building Qt 5.6 - multiply defined symbols

Trying to build Qt 5.6 from source on Windows 7 using MSVC13. Compilation goes fine, but I get two linker errors regarding multiply defined symbols:
Qt5SerialBus.lib(qmodbusdevice.obj) : error LNK2005: "class QLoggingCategory const & __cdecl QT_MODBUS(void)" (?QT_MODBUS##YAAEBVQLoggingCategory##XZ) already defined in mainwindow.obj
Qt5SerialBus.lib(qmodbusdevice.obj) : error LNK2005: "class QLoggingCategory const & __cdecl QT_MODBUS_LOW(void)" (?QT_MODBUS_LOW##YAAEBVQLoggingCategory##XZ) already defined in mainwindow.obj
release\adueditor.exe : fatal error LNK1169: one or more multiply defined symbols found
I suspect the problem (and solution) are similar to this one in which the offending functions can just be marked inline, however I can't find the definitions for these two functions anywhere in the modbus code. Does anyone know where "QT_MODBUS" and "QT_MODBUS_LOW" are defined, or is there another workaround to this?

Compilation Errors in Visual Studio 2012

I'm been trying for the last couple of hours to solve an error that I can't get rid of. Here it is the gist: https://gist.github.com/pluralism/11294490
I'm out of ideas because everything looks correct for me. The compilation error I'm getting is the following:
1>MenuCAL.obj : error LNK2005: "void __cdecl printSquareArray(int * *,unsigned int)" (?printSquareArray##YAXPAPAHI#Z) already defined in calproject.obj
1>C:\Users\Andre Pinheiro\Desktop\calproject\Debug\calproject.exe : fatal error LNK1169: one or more multiply defined symbols found
If you need other files please feel free to ask. Thanks in advance!
If you want to define printSquareArray in a header, mark it inline to allow a definition in every translation unit that includes the header.
Alternatively, move the definition into a source file, so it's only defined once.

C++ linkage error

I compile in Visual studio 2008 and get this error. I have researched linkage error but am still uncertain to what it is. This is the finished code to a poker game so I would rather not post the code. Can someone translate this error message for me?
error LNK2019: unresolved external symbol "void __cdecl betFold(double)" (?betFold##YAXN#Z) referenced in function "void __cdecl flopAction(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?flopAction##YAXV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###Z) OH-DLL.obj
Your function void flopAction(std::string arg) uses a function betFold(double) that gets referenced and declared in some header, but is not implemented so that the linker is able to find it.
It means that you have declared this method but not defined it. Or at least the linker cannot find the definition, either because it’s in a library that you didn’t reference or else because it’s in an object file (source file) that is not part of your build process.
Sound like you forgot to specify the *.lib file that belongs to the *.dll. You can edit the list under your Project Property Pages -> Configuration properties -> Linker -> Input, remember to do this for the Debug and Release configuration.
And please try to refrain from phrases like wtf etc :)
Also, you could check your signature(function declaration), so that it contains only the type in it's parameter lists, while inside the definition(.cpp file), it contains both the type and parameter names. For eg,
in the .h file where the declaration sits:
void myfunc(int, char*);
and in the .cpp file where the definition sits:
void myfunc(int num, char* name)
{
//
}
I learnt this before in my college, but don't sure if Dev C++ supports it, left this things for a long time ago, just using Borland at that time.
hope this helps.
thanks.

how to ignore LNK2005 & LNK1169?

So I have a Visual Studio 2010 project that uses external libraries and in order to get it compile without LNK2005 I had to juggle arround with the order of the libraries in the linker settings.
I got it to compile fine in release mode, but for whatever reasons I don't manage to get it to compile without LNK errors in debug.
Is there no way to generally ignore LNK2005 and tell the linker to simply use whatever he encounters first?
Thanks!
//edit: here are some of the errors output of the PARTICULAR problem. however I already tried to solve that in different ways with each solution giving me different linker problems. hence i'm looking for general solution to ignore LNK2005
Error 7 error LNK2005: "private: __thiscall type_info::type_info(class type_info const &)" (??0type_info##AAE#ABV0##Z) already defined in Libcmtd.lib(typinfo.obj)
...\msvcprtd.lib(MSVCP100D.dll)
Error 8 error LNK2005: "private: class type_info & __thiscall type_info::operator=(class type_info const &)" (??4type_info##AAEAAV0#ABV0##Z) already defined in Libcmtd.lib(typinfo.obj)
...\msvcprtd.lib(MSVCP100D.dll)
Error 9 error LNK2005: _exit already defined in Libcmtd.lib(crt0dat.obj)
...\msvcprtd.lib(MSVCP100D.dll)
Error 10 error LNK2005: __invalid_parameter already defined in Libcmtd.lib(invarg.obj)
...\msvcprtd.lib(MSVCP100D.dll)
...
Error 37 error LNK1169: one or more multiply defined symbols found
You may try the linker-option /FORCE (Force File Output in the Linker General tab of the Project Properties). This will force the linker to create a exe/dll even when such errors occur. But its left to you to find out if this exe does work at all or even correctly. After all i would not recommend this strategy.
Linker errors can sometimes be tedious to solve, but usually it has to be done only after migrating or setting up a project. This may take quite a while - it sometimes took me more then a day, but it should be done properly.
You absolutely must not ignore linker errors, ever! A linker is telling you that it's confused about a symbol that's defined in multiple places - where should it take the definition from? Do you really want it to be arbitrary? What about when you change your code and the linker randomly decides to take the other definition which might suddenly break your code?
Instead of fighting the tool, correct your code so that it compiles and links without errors. This MSDN article has some information on fixing it, along with links for more information.

Linking error in C++ - implementing a indexList

Linking...
Directory.obj : error LNK2019: unresolved external symbol "public: void __thiscall indexList<class entry,100>::read(class std::basic_istream<char,struct std::char_traits<char> > &)" (?read#?$indexList#Ventry##$0GE###QAEXAAV?$basic_istream#DU?$char_traits#D#std###std###Z) referenced in function _main
Getting this error and others associated with indexList implementation. I have included all the right files, not sure what this means?
indexList.h
indexList.cpp
Also, using VS .NET 2003 - They are under the "Source Files" and "Header Files" However, I tested with deleting the indexLish.h and the error doesn't change?
What you have is a class template. This means when the compiler needs to call a function, it will look at your template definition and generate the corresponding code as needed.
For example, the following probably has a compile-time error in it if tried to call it:
template <typename T>
void doSomething(const T& x) {
x->_this_function_does_not_exist_ever_();
}
But as long as you don't call doSomething you won't get errors.
The problem you have is that your header file tells the compiler "hey, these functions exist", but when the compiler tries to generate them it cannot find any definitions. (You cannot "compile" definitions in a source file and link them in, they must be visible to the caller.)
The most common solution is to simply define the entire class template in the .h or .hpp file.
Are you using visual studio then include both the files into the solution and then run.
Since you are using templates, the best way is to include the definition in .H file.
I read something from this book . And here is something it may help you too.