I’m trying to write the global function:
std::ostream& operator<<(std::ostream& out, const Str& str)
{
for(int i = 0; i < (int)str.mSize; ++i)
out << str.mBuffer[i];
return out;
}
For a custom string class. It compiles fine but when I go to link:
1>Str.obj : error LNK2005: "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Str const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABVStr@@@Z) already defined in Main.obj
1>C:\Users\Ron\Documents\Visual Studio 2010\Projects\App\Debug\App.exe : fatal error LNK1169: one or more multiply defined symbols found
How could multiple definitions for this exist? I just created the class Str.
If you define a function in a header file and include it twice, you’ll get a multiple definition error, which you have.
To remedy this, declare the function in the header with a prototype and define it in a
.cppfile.Alternately, if you’re trying to make a header only library, you could do