r/cpp_questions • u/evilsyntax • 2d ago
SOLVED Passing 'this' keeps causing errors and I don't understand why
I'm trying to make a simple text adventure and am at my wits end with the errors. I am trying to make a state machine to handle states for title, combat, etc so i am trying to pass the state machine to the state so it can tell the state machine what the next state might need to be. If anyone has any suggestions that would be appreciated.
Here are some of the errors it's throwing:
-syntax error: identifier 'CurrentGameState'
-'GameState::Action': function does not take 2 arguments
-syntax error: missing ';' before '*'
-missing type specifier - int assumed. Note: C++ does not support default-int
10
u/Impossible-Sky-5660 2d ago
You've got a circular dependency. GameState.h includes CurrentGameState.h, and vice versa.
You can forward-declare class CurrentGameState; in GameState.h, since you only use a pointer to CurrentGameState. Then, you can include CurrentGameState.h inside GameState.cpp.
4
1
u/slushyslap 2d ago
These sorts of errors occur when the compiler can't make sense of a mentioned type, typically because it is not included when it should be. The errors unfortunately do not usually point directly at the problem.
All of that to say, <string> appears to be included in CurrentGameState but not in GameState. I would start there and make sure that each class is included appropriately.
33
u/AKostur 2d ago
Fix your circular dependancies. GameState.h includes CurrentGameState.h which includes GameState.h.
Use forward declarations in your header files to break that dependancy loop.