0

I am referencing 2 classes with themselves using forward referencing however I am still getting errors with my class instance declarations in each class. Note: I'm using DirectX and version control if that has anything to do with it?

Game.h:

#ifndef GAME_H
#define GAME_H

class Player;

class Game {
public:
     Player player;  // Undefined class error here
//...
};

#endif

Player.h:

#ifndef PLAYER_H
#define PLAYER_H

class Game;

class Player {
public:
    Game game;  // Undefined class error here
//...
};

#endif

Obviously there is a lot more code but I thought including only essential code would make it easier for you to read.

Any help would be greatly appreciated.

Many thanks

Ash

1 Answers1

0

In order to declare a member of a specific type, the compiler must have the full declaration of that type at the time:

class Player;

class Game {
public:
      Player player; // dunno what this means

You have created an unfortunate circular dependency which you can only solve with either pointers or references:

// Game.h
#include "player.h"

class Game {
    Player player; // ok
};

// Player.h

class Game;

class Player {
    Game* game;  // ok.
};
kfsone
  • 23,617
  • 2
  • 42
  • 74
  • Yeah I've read a few forum solutions who have said the same thing however I still get errors when I do your solution^. It now says "pointer to incomplete class type not allowed" when I reference game in my cpp file, any idea what the problem is now without me having to post a load of code? – Ashley Gibson Mar 19 '16 at 22:30