Download

Proper way to do Forward Declaration?

Hi, I have two classes I need to be able to communicate with eachother. A character, and an animinstance.
I wish to trigger an animation in the animinstance from the character (this part I have done without issues by doing an #include and getting the animinstance in my character), then when the animation is done in the animinstance, I wish to in turn trigger a function in my character. Now I cannot just do #include and get the character again, as that would result in a circular dependency loop.

The best way around this appears to be a forward declaration within my animinstance, but I cannot quite seem to find good examples of the right syntax for this and haven’t done this before.

This is what I got so far
animinstanceheader.h:


    class ATribesmenCharacter;
    ATribesmenCharacter* Char;

cpp:


UCharAnimInstance::UCharAnimInstance(const FObjectInitializer& ObjectInitializer)
    : Super(ObjectInitializer)
{
    Char = Cast<ATribesmenCharacter>(TryGetPawnOwner());

}

But I get the following error:


use of undefined type 'UCharAnimInstance::ATribesmenCharacter'  


If you dereference the ptr, you have to include the header.
In your cpp file the Cast<> function is what is forcing you to include the header in cpp file.

You can include each other’s headers in cpp file, you can’t do it on headers.

Oh I see, I didn’t realize there was a distinction between the header and cpp like that.

That makes things way easier. Thanks!