1

I have made a function which looks like this:

//hpp
extern std::array<CPlayer, MAX_PLAYERS> PlayerArray;
inline CPlayer& Player(int playerid);
//cpp
std::array<CPlayer, MAX_PLAYERS> PlayerArray;
inline CPlayer& Player(int playerid)
{
    return PlayerArray[playerid];
}

but when doing this I get a linker error saying:

error LNK2001: unresolved external symbol "class CPlayer & __cdecl Player(int)" (?Player@@YAAAVCPlayer@@H@Z)

However when I remove the inline keyword, everything compiles fine.

Why does this happen?

Gizmo
  • 1,990
  • 1
  • 24
  • 50

2 Answers2

2

The function get's inlined and effectively removed from the binary as a stand alone function.

Move the implementation to header to solve this.

egur
  • 7,830
  • 2
  • 27
  • 47
0

From C++ FAQ best practice, if function body definition is outside class, then remove inline keyword from declaration.

class Foo {
public:
  void method();  ← best practice: don't put the inline keyword here
  ...
};

inline void Foo::method()  ← best practice: put the inline keyword here
{ ... }
Digital_Reality
  • 4,488
  • 1
  • 29
  • 31