1

I've got the following function defined in chess_location.h:

inline chess_location operator+(chess_location lhs, const chess_coord& rhs);

and then, in chess_location.cpp:

#include "chess_location.h"

chess_location operator+(chess_location lhs, const chess_coord& rhs) { 
  //function definition
}

and then am using this operator in my main() in main.cpp, like so:

#include "chess_location.h"
int main() {
  chess_location_B = chess_location_A + chess_coord;
}

but, I'm getting a linker error saying that the operator couldn't be found:

error LNK2019: unresolved external symbol "class chess_location __cdecl operator+(class chess_location,class chess_coord const &)" (??H@YA?AVchess_location@@V0@ABVchess_coord@@@Z) referenced in function _main

It seems to me like the linker isn't connecting the declaration of the operator to the definition, but I'm not sure why. I suspect that I may have something wrong with my consts. If I move the operator definition to main.cpp, however, everything compiles and works fine.

Any idea where this error is coming from?

Fabian Tamp
  • 4,416
  • 2
  • 26
  • 42

1 Answers1

7

If operator + should be inline, then you need to put the definition in the header file. If it is not intended to be inline, then put it in the cpp file and remove "inline" from the declaration.

Werner Henze
  • 16,404
  • 12
  • 44
  • 69