Forward declaration issues

Hi there,

I’ve read Rama’s post on forward declarations but am having an issue I think I’m completely missing a basic fundamental here and I’m probably doing this the wrong way, any help would be much appreciated:

I have two classes:

AFood (APawn)
AHero (ACharacter)

in AHero I want a list of food in the players view, then for each food item I want them to move(I know how crazy this probably sounds from a game design perspective but please bear with me!). In both classes I want to cast to the specific class and use functions from both, here’s what I have:

AHero.h

GENERATED_BODY()
class AFood;

...

protected:
TArray<AFood*> VisibleFood;

AHero.cpp

#include "TheFoodGame.h"
#include "Hero.h"
#include "Food.h"

AHero::AHero()

...

void AHero::CheckFood()
{
    TTransArray<AActor*> AllActors = GetWorld()->PersistentLevel->Actors;

    for (AActor* CurrentActor : AllActors)
    {

        if (CurrentActor != nullptr && Cast<AFood>(CurrentActor)) {
            AFood* CastedActor = Cast<AFood>(CurrentActor);

            FVector CurrentActorLocation = CurrentActor->GetActorLocation();
            FVector OurLocation = GetActorLocation();
            FVector OurForwardVector = GetActorRotation().Vector();

            FVector TempVec = CurrentActorLocation - OurLocation;
            TempVec.Normalize();

            float DotProduct = FVector::DotProduct(OurForwardVector, TempVec);

            if (DotProduct > 0.7f) {
                VisibleFood.Add(CastedActor);
            }
        }

    }
}

That will give me an “use of undefined type ’ AHero::AFood’” error as is, removing the #include on the cpp doesn’t change this. this is before attempting to do anything with “CastedActor”, Ideally I’ll like to call CastedActor->MoveFood(); which would in AFood::MoveFood(); cast the player to AHero for some information and then move to it. using just includes however would net me circular dependencies.

It looks like you’re doing the forward declaration inside AHero’s class declaration; it should go outside it (so before the UCLASS macro). Doing it inside AHero’s declaration makes the compiler think it’s a class inside AHero’s scope (hence AHero::AFood instead of it just saying AFood).

That was it, thank you very much!