I have a base class, which is declared in the following header file:
#pragma once
#include "StateHandler.hpp"
namespace ta {
class GameState {
public:
// ...
};
} /* ta */
Then, I have two children, which look like this:
#pragma once
#include "../GameState.hpp"
namespace ta {
class DefaultState: public ta::GameState {
public:
// ...
};
} /* ta */
and
#pragma once
#include "../GameState.hpp"
namespace ta {
class WorldMapState: public ta::GameState {
public:
// ...
};
} /* ta */
When trying to compile that, I get errors, that GameState
was not declared in the second of the two children I include. When I remove the #pragma once
from the GameState.hpp
, it says that GameSate
gets redefined. I get why that happens, but I can't find a way to fix that.
UPDATE: Using include-guards doesn't work either. I get the following error, when using #pragma once
or include-guards:
In file included from /[...]/include/statemachine/gamestates.hpp:2,
from /[...]/include/common.hpp:4,
from /[...]/include/statemachine/gamestates/../StateHandler.hpp:5,
from /[...]/include/statemachine/gamestates/../GameState.hpp:4,
from /[...]/include/statemachine/gamestates/DefaultState.hpp:4,
from /[...]/include/statemachine/gamestates.hpp:1,
from /[...]/include/common.hpp:4,
from /[...]/source/main.cpp:1:
/[...]/include/statemachine/gamestates/WorldMapState.hpp:7:47: error: expected class-name before '{' token
class WorldMapState: public ta::GameState {
^
And this is the error I get, when I don't use include-guards or #pragma once
in the GameState.hpp
(this error appears 3 times):
In file included from /[...]/include/common.hpp:5,
from /[...]/source/main.cpp:1:
/[...]/include/statemachine/GameState.hpp:4:11: error: redefinition of 'class ta::GameState'
class GameState {
^~~~~~~~~
In file included from /[...]/include/statemachine/gamestates/WorldMapState.hpp:4,
from /[...]/include/statemachine/gamestates.hpp:2,
from /[...]/include/common.hpp:4,
from /[...]/include/statemachine/gamestates/../StateHandler.hpp:5,
from /[...]/include/statemachine/gamestates/../GameState.hpp:1,
from /[...]/include/statemachine/gamestates/DefaultState.hpp:4,
from /[...]/include/statemachine/gamestates.hpp:1,
from /[...]/include/common.hpp:4,
from /[...]/source/main.cpp:1:
/[...]/include/statemachine/gamestates/../GameState.hpp:4:11: note: previous definition of 'class ta::GameState'
class GameState {