I’m trying to create a UPROPERTY TArray of a custom Enum. This Enum is defined in a different header file, and I want to forward declare it. If I don’t make it a UPROPERTY, everything compiles. It I add the UPROPERTY macro, it fails. Here’s the error:
error: In GridSquare: Unrecognized type 'EVesselType'
By the way, I’m using C++11 style enums.
Here’s the code:
Vessel.h
#pragma once
#include "GameFramework/Actor.h"
#include "Vessel.generated.h"
UENUM()
enum class EVesselType : uint8
{
// Try to keep this alphabetical
Apple,
Banana,
Cherry,
Undefined
};
UCLASS()
class PROJECT_API AVessel : public AActor
{
GENERATED_BODY()
public:
.
.
.
GridSquare.h
#pragma once
#include "GameFramework/Actor.h"
#include "GridSquare.generated.h"
enum class EVesselType : uint8; // Forward Declaration
UCLASS()
class PROJECT_API UGridSquare : public UStaticMeshComponent
{
GENERATED_BODY()
public:
UGridSquare(const FObjectInitializer& ObjectInitializer);
private:
UPROPERTY(Category = Grid, EditAnywhere)
TArray<EVesselType>* VesselsContained;
};
If I remove the UPROPERTY line, it compiles fine. Any ideas?
While in theory that would work, I’m trying to avoid circular dependency. Thats why I’m using forward declaration. Including the header would defeat the point.
enum can’t be forward declared because the values must be known at compile time so the appropriate size of memory is allocated. Just create separate .h file or instead of using the enum it self you can use a type of enum unreal supports only type of uint8 so you can declare that in your .h instead
Circular dependency arises when you include headers that are dependent on each other. The standard practice is to include these headers inside the .cpp file.
I’m still seeing this issue in 4.17. It would be really great to get a fix for this. Forward declaration already works with USTRUCTs and UCLASSes, so I feel it should work with UENUMs too.
Yes, it hasn’t to do with allocation. Forward declaration of enum with size or enum class (with size) is well supported in C++0x. However UE4 doesn’t allow this when used with UENUM(). Maybe b/c with UENUM() the enum class is converted to some kind of class that is not compatible with the forward declared type anymore?