You are using [source] tags. You should be using [code] tags.EDIT: Btw, I have no idea what's going on but the <code> feature on the forums won't work right for me... At least it's showing my code all comes out in one, mangled line in my above post...
You did not fully qualify the definition of the operator in the .CPP file.1) Global operators and linker errors ::
Why does it cause a linker error to prototype a global operator in a header file then implement it in a source file, like so::// point.h :: Point operator +( const Point& p1, const Point& p2 ); Point operator -( const Point& p1, const Point& p2 ); // point.cpp :: Point operator +( const Point& p1, const Point& p2 ) { Point result = p1; return ( result += p2 ); } Point operator -( const Point& p1, const Point& p2 ) { Point result = p1; return ( result -= p2 ); }
Error 1 error LNK2019: unresolved external symbol "struct ATCFramework::Drawing::Point __cdecl ATCFramework::Drawing::operator+(struct ATCFramework::Drawing::Point const &,struct ATCFramework::Drawing::Point const &)" (??HDrawing@ATCFramework@@YA?AUPoint@01@ABU201@0@Z) referenced in function _wmain C:\Users\ATC\Documents\Visual Studio 2012\Projects\ATCWARE\src\ConsoleTest\main.obj ConsoleTest
(NOTE:: I also get LNK1120, unresolved external symbols, of course...)
But if I just move the implementation of the operators into the header file it compiles and runs fine. What the deal??
It should be:
ATCFramework::Drawing::Point ATCFramework::Drawing::operator + ( const Point & p1, const Point & p2 ) {
Point result = p1;
return ( result += p2 );
}L. Spiro