I’m having trouble with what is apparently a linker error (‘undefined reference’) in Eclipse / C++. All the classes shown below compile fine, except for one, PlayGame.cpp, which is giving the ‘undefined reference’ error (also shown below).
Below are the relevant classes and pieces of code.
PlayerFactory.h
PlayerFactory.cpp
Game.h
Game.cpp
// constructor for game: Game::Game (const PlayerFactory& factory) { cout << ' constructor' << endl; }
PlayGame.cpp
// start of code for game where error occurs #include 'Game.h' #include 'PlayerFactory.h' int main() { try { PlayerFactory factory; Game game (factory); <== undefined reference error ...
The above line gives the error ‘undefined reference to `Game(PlayerFactory const&)”
What’s causing this error, and how can it be corrected?
The default visibility for
classdeclarations is private. So all the member functions of bothPlayerandPlayerFactoryclasses areprivate— not accessible by clients. You need to make them public.Player.h
PlayerFactory.h
Also, the
Game::Play()lacks areturnstatement.Do add the required headers, forward declarations and
usingstatements as required (I skip them here).