While working on a little game of mine, I ran into a circular dependency related issue.
Say I'm using 2 classes and a helper namespace(all include guarded), where Base is the main game class, Player the player object and a child of Entity, and helper a functional namespace responsible for handling tile collision:
When I try to compile the code, Player is not recognized as a type-specifier in base.h and I also get an unknown override specifier. When I forward declare Player it is undefined, although I am including player.h. I've been questing on the internet for a solution, but none of the typical solutions to circular inclusion seemed applicable. As aforementioned I have tried all sorts of combinations of forward-declarations but it only changed the type of error.
I was wondering whether anyone could notice any flaw in the code below.
(Please note that I've put all of the function implementations in the headers for demonstration purposes only, they've got their own little cpp files)
base.h
#include "player.h"
#include "level.h"
class Base{
static Base& instance()
{
static Base base;
return base;
}
Player player;
Level level;
}
entity.h
class Entity{
vec2 velocity; //Assume we are using the glm library
vec2 position;
virtual void update()
{
}
}
player.h
#include "helper.h"
#include "entity.h"
class Player:public Entity{
void update()
{
velocity=helper::tileCollision(velocity);
//Update position and stuff
}
}
helper.h
#include "level.h"
#include "base.h"
namespace helper{
vec2 tileCollision(vec2 velocity)
{
Level& level = Game::instance().level;
//Pop some fancy tile collision math in here
return vec2(0);
}
}