TArray of custom class..."not a valid template"

Cannot declare a TArray of type AIEnemy in AIHordeContainer. Not sure what I need to do.

First…the class.



#pragma once

#include "GameFramework/Character.h"
#include "AIHordeHandler.h"
#include "AIHordeContainer.h"
#include "AIEnemy.generated.h"

/**
 * 
 */
UCLASS(abstract)
class AAIEnemy : public ACharacter
{
	GENERATED_UCLASS_BODY()
	
	AAIHordeHandler * HordeHandler;
	AAIHordeContainer * HordeContainer;

	enum AIBehavior {WANDER};
	enum AIState {NORMAL};
	enum AIPersona {LONER, HORDE};	
	AIBehavior behavior;
	AIState state;
	AIPersona persona;

	AActor * DetectedTarget;

	virtual void OnConstruction(const FTransform& Transform) OVERRIDE;
	bool GetInHorde();
	bool GetIsHorde();
};


Creates a Container to hold members of horde when Horde type Enemies interact.



void AAIHordeHandler::Tick(float DeltaSeconds)
{
	Super::Tick(DeltaSeconds);
	//GEngine->AddOnScreenDebugMessage(-1, 2, FColor::Red, TEXT("WORKING"));

	UWorld* const World = GetWorld();
	for (TObjectIterator<AAIEnemy> Itr; Itr; ++Itr)
	{
		for (TObjectIterator<AAIEnemy> ItrOther; ItrOther; ++ItrOther)
		{
			float itrDist = Itr->GetDistanceTo(*ItrOther);
			if (Itr->GetName() != ItrOther->GetName() && itrDist <= 750) {
				if (Itr->GetIsHorde() && ItrOther->GetIsHorde())
				{
					if (!Itr->GetInHorde() && !ItrOther->GetInHorde()){
						Itr->HordeContainer = World->SpawnActor<AAIHordeContainer>();
						ItrOther->HordeContainer = Itr->HordeContainer;
						//Itr->HordeContainer->AddMember(*ItrOther);
						//Itr->HordeContainer->AddMember(*Itr);
						//GEngine->AddOnScreenDebugMessage(-1, 2, FColor::Red, Itr->GetName());
						//GEngine->AddOnScreenDebugMessage(-1, 2, FColor::Red, ItrOther->GetName());
					}
					else if (!Itr->GetInHorde() && ItrOther->GetInHorde()){

					}
					else if (Itr->GetInHorde() && !ItrOther->GetInHorde()){

					}
					else if (Itr->GetInHorde() && ItrOther->GetInHorde()){
						//Check if in same Horde
						//Merge Hordes or steal members
					}
				}
			}
		}
	}
}

AIHordeContainer.h…cannot #include “AIEnemy.h”…results in an error. Cannot declare a TArray of type AIEnemy.



#pragma once

#include "GameFramework/Actor.h"
#include "Array.h"
#include "AIHordeContainer.generated.h"

/**
 * 
 */
UCLASS()
class AAIHordeContainer : public AActor
{
	GENERATED_UCLASS_BODY()

	TArray<AAIEnemy> HordeMembers;
	uint8 HordeSize;
};


What error? You need to declare a class before using it, which means including the header file.

Oh, you seem to have circular dependencies. Don’t include AIHordeContainer.h in AIEnemy.h, but replace it with a forward declaration like this:


class AAIHordeHandler;

alg0801,

You probably want to keep an array of pointers, not an array of AAIEnemy objects:

Raw C++ pointers circumvent the garabage collector, so if your enemies get destroyed, these pointers may actually point to invalid memory. If you intend to keep the TArray around for a while, you should probably use weak object pointers instead:

Now, if an enemy gets destroyed, the weak pointer will simply be nullptr. When you iterate the array, make sure to check that the pointers are still valid, i.e:

Forward declaration fixed it…also looked over my dependencies and cleaned it up.

Definitely wanted a pointer gmpreussner Thanks.