Forward Declaration for Structure

I can easily use forward declarations on classes, but the same mechanism does not work on structures. Let’s say that in a single header file, I wrote this:

struct FStructA;
struct FStructB;

USTRUCT(BlueprintType)
struct FStructA
{
	GENERATED_USTRUCT_BODY()

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Data")
	FStructB TestB;
};

USTRUCT(BlueprintType)
struct FStructB
{
	GENERATED_USTRUCT_BODY()

	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Data")
	FStructA TestA;
};

When I compile it, the compiler always fail with the following message: “Unrecognized type ‘FStructB’ - type must be a UCLASS, USTRUCT or UENUM”. This sort of error does not happen in ordinary C++. Is this a bug in UE or did I do something wrong?

You cannot forward declare a class or struct if you don’t use it as a pointer. Pointers have fixed sizes so the compiler can allocate enough space for it. Non-pointers, on the other hand, are variable sizes and the compiler doesn’t know how much memory to allocate at compile time.

Your illegal line is line 10 where you have attempted to use an incomplete class type. Change the variable to a pointer. If you do not change it to a pointer you will not be able to compile two structs that have a strong dependence on each other.

To expand a bit on what Shohei is saying. This is how structs work in UE4.

The absolute minimum you need is:

USTRUCT( )
struct FTestingStructA
{
	GENERATED_BODY( );
};

This includes having “F” before the name of the struct, or you will get a compiling error of, “Struct ‘TestingStructA’ has an invalid Unreal prefix, expecting ‘FestingStructA’”.

You need GENERATED_BODY( ), or you will get the compiling error of, “Expected a GENERATED_BODY() at the start of struct”.

Thank you for submitting a bug report, however at this time we believe that the issue you are describing is not actually a bug with the Unreal Engine, and so we are not able to take any further action on this. If you still believe this may be a bug, please provide steps for us to reproduce the issue, and we will continue our investigation.

Thanks for the explanation. Shohei mentioned that the struct must be a pointer if I want to forward declare it. But USTRUCT cannot be treated as pointers can they? The compiler always give me an error if I created a pointer to a USTRUCT. In that example if I declare this, in another class (as UPROPERTY):

FTestingStructA* pTestA;

then it won’t compile.