Defining Variable With StaticClass() for an Undefined Class

Hello there,

So I’m having a bit of trouble looking for

UCLASS()
class MYPROJECT_API AMyActor : public AActor
{
   GENERATED_BODY()

   TSubclassOf<UMyClassObject> Derived1 = UMyClassObject_Derived1::StaticClass();
   TSubclassOf<UMyClassObject> Derived2 = UMyClassObject_Derived2::StaticClass();
};

UCLASS(Abstract)
class MYPROJECT_API UMyClassObject : public UObject
{
    GENERATED_BODY()
};

UCLASS()
class MYPROJECT_API UMyClassObject_Derived1 : public UMyClassObject
{
    GENERATED_BODY()
};

UCLASS()
class MYPROJECT_API UMyClassObject_Derived2 : public UMyClassObject_Derived1
{
    GENERATED_BODY()
};

My issue here is that the compiler will throw an error because the classes haven’t been defined yet.

I am aware that you use forward declarations for declaring unspecified class types. But once you attempt to initialize the variable with a class not yet defined, that method goes out the window, since the compiler has no idea what StaticClass() IS yet.

Any ideas for how to handle this? Thank you in advance :slight_smile:

You’ll need to initialize those variables in a constructor, defined in a cpp file. That way you have the full definition of the derived classes available in order to use the ::StaticClass function.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.