Circular dependencies

I’ll briefly expand on Kelby’s post
controller.h:

class FMyStateMachine; // forward-declaration

class FMyController {
    FMyStateMachine* StateMachine = nullptr; // pointer is fine

    void DoThing(FMyStateMachine& StateMachine); // reference is also fine

    inline void DoInlineThing() {
        StateMachine->Transition(); // NOT OK, using undefined type (compiler doesn't know about Transition() function). Function implementation needs to be in the cpp file.
    }
};

statemachine.h:

class FMyController; // forward-declaration

class FMyStateMachine {
    FMyController* Controller = nullptr;
    void Transition();
}

If you want say a structure that’s contained directly inside some other class or struct (not just referenced), you’ll need to include the file it’s defined in. You can still forward-declare the parent class in the struct’s header if you need a reference from struct to parent.

1 Like