Forward declaration not working on UPROPERTY enum

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?

Hello, repwolfe

Please make sure that you have appropriate include for .h file of the enum, which in your case should look like this:

#include "Vessel.h"

Also, please note that you can’t have an exposed pointer of TArray type, so your declaration should look something like this:

UPROPERTY(Category = Grid, EditAnywhere)
TArray<EVesselType> VesselsContained;

Hope this helped!

Have a great day!

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.

Create a separate header file for just the enum and include that in your header file.

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

I’m with you on this one. Did you manage to get around the problem?

My project compiles ok when I forward declare as you have done…

enum class EMyEnum : uint8; 

It only failed when I copied your code and put it into a TArray with a pointer, but the error wasn’t regarding the enum…

Inappropriate '*' on variable of type 'TArray', cannot have an exposed pointer to this type

Are you sure you want a pointer on your TArray? Because other than that it works for me (with a UPROPERTY)

1 Like

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.

+1 why can’t we have forward declared enum in UFUNCTION? Like this doesn’t compile:

UFUNCTION(BlueprintCallable, Category = "World|Interior")
EMInteriorType GetInteriorType() const { return InteriorType; }

but if I comment UFUNCTION() it compiles… where the EMInteriorType was forward declared at the top of my .h

Well the forward declaration tells the size no?
enum class EMyEnum : uint8;

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?

Just in case:
I had my ‘PROJECT_API’ in lowercase and generated same error.
Changed it in my entire project to uppercase solved my problem. :slight_smile: