game.cpp
#include "game.h"Game::Game() : tileMap(mapRows, mapCols), player(this),{...}
When instantiating the Game class, I pass a pointer reference of this class to player.
When instantiating player I want to use a function within tileMap
However I cannot access game pointer due to it being an incomplete type.
Why can't I access it?
IGameActor.h
class Game; // Forward declaration of Game classclass IGameActor {protected: Game* game; // Pointer to the Game objectpublic: virtual ~IGameActor() {} IGameActor(Game* gamePtr) : game(gamePtr) {} IGameActor() : game(nullptr) {}
player.h
class Player : public IGameActor { public: Player(Game* game); virtual void update(float dt) = 0;}
player.cpp
Player::Player(Game* game) : IGameActor(game) { ... }void Player::update() { std::cout << game->name;}
./build.shsrc/entities/player.cpp: In member function ‘virtual void Player::update(float)’:src/entities/player.cpp:54:22: error: invalid use of incomplete type ‘class Game’ 54 | std::cout << game->name; | ^~In file included from include/entities/player.h:5, from src/entities/player.cpp:1:include/entities/IGameActor.h:7:7: note: forward declaration of ‘class Game’ 7 | class Game; // Forward declaration of Game class | ^~~~Compilation failed.