I’m forward declaring my struct here in VoxelizedActor.h:
#pragma once
#include "GameFramework/Actor.h"
#include "TheGrid.h"
#include "Grabbable.h"
#include "VoxelizedActor.generated.h"
struct FAxisOrientation;
...
UCLASS()
class VRGAME_API AVoxelizedActor : public AGrabbable
{
GENERATED_BODY()
...
private:
UPROPERTY()
FAxisOrientation gridOrientation;
};
And I define it at the top of TheGrid.h:
#pragma once
#include "GameFramework/Actor.h"
#include "VoxelizedActor.h"
#include "TheGrid.generated.h"
USTRUCT(BlueprintType)
struct FAxisOrientation {
GENERATED_BODY()
public:
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="AxisAlignedOrientation")
uint8 x; // axis * 90 = degree
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="AxisAlignedOrientation")
uint8 y;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="AxisAlignedOrientation")
uint8 z;
FAxisOrientation() : x(0), y(0), z(0) {}
FAxisOrientation(uint8 X, uint8 Y, uint8 Z) : x(X), y(Y), z(Z) {}
};
...
But I keep getting the error: “VoxelizedActor.h(71): Unrecognized type ‘FAxisOrientation’ - type must be UCLASS, USTRUCT, or ENUM”
As far as I can tell, this is a circular dependency issue, but I’m not sure what I’m doing wrong. Can anyone help?
The reason it fails is because when declaring a field, the compiler must know the size of that type. While the compiler doesn’t know the size of FAxisOrientation
, you could declare a field of type FAxisOrientation *
instead.
If I were you, I would ditch the forward declaration and reverse the direction of includes: i.e. remove #include VoxelizedActor.h
from your struct header and add #include TheGrid.h
to the top of your class header.
Whoops - it seems you already #include TheGrid.h
in your class header. But because you have circular includes, this include doesn’t actually do anything at the moment.
Hey Soup,
You have 2 issues:
The circular Includes.
And the incomplete type
Circular Includes appear when you have 2 classes:
A and B
//A.h
#include "B.h"
//B.h
#include "A.h"
This means that A.h includes itself which includes itself…and so on. The same goes with B
Incomplete Types:
If you use Member which are defined as Values (not Pointers), you have to include these because they have to have access to the default Constructor.
FAxisOrientation Orientation; //Calls Default Constructor.
If you want to have a member or type FAxisOrientation it has to be a pointer OR you have to #include the .h file which defines this struct.
Greetings
Thanks! This helped a lot. I switched the forward declaration around, and now everything works. And I got rid of the extra include - I hadn’t realized I only needed one when doing forward declarations.