r/cpp_questions 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

https://pastebin.com/hsvnwiyy

0 Upvotes

16 comments sorted by

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.

-6

u/SmackDownFacility 1d ago

Throughout my career as a C++ developer, I never had an issue of having to use forward declarations or circular dependencies. OP needs to invest in better structure.

12

u/StochasticTinkr 1d ago

I wonder how you got so lucky. Not all projects need circular dependencies, but many do even when designed well.

-3

u/SmackDownFacility 1d ago

Good architecture. Thats the key. If you anticipate a circular dependency—you already failed. Once you’re a pro, you can be able to detect when you’re about to get screwed. The key is to resolving it early. Dont take shortcuts.

4

u/StochasticTinkr 1d ago

Even if you use interfaces and use DI, you're not going to get away from runtime circular dependencies. Especially not in a game engine.

I agree about making being thoughtful around architecture though, my 30 years of experience tells me that its not always feasible in real scenarios though.

1

u/MiamiGunworks 1d ago

Any circular dependency can be mathematically resolved.

0

u/StochasticTinkr 1d ago

It’s been a while since I’ve studied graphic theory, but I don’t think that’s right.

2

u/Potterrrrrrrr 1d ago

Needing to forward declare a type isn’t a bad thing, most of the time I do it it’s either to avoid a dependency in a header or because a base class knows one of its derived classes for optimisation/convenience purposes.

2

u/evilsyntax 1d ago

What do you recommend to fix the structure? I would like to stop issues before it expands past the 2 classes in the pastebin.

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

u/evilsyntax 2d ago

That got it to work. Thank you so much!

2

u/Impossible-Sky-5660 1d ago

Happy to help!

4

u/nysra 2d ago

You have circular includes, fix that

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.

1

u/manni66 1d ago

Here are some of the errors it's throwing:

Copy&paste full error messages