TSubclassOf sometimes fails to compile for forward declared classes.

I’ve seen code written by Epic themselves like this:

UPROPERTY(Stuff goes here)
TSubclassOf<class ASomeActorClass> SomeClass;

And sometimes it works for me too.

Other times I get errors like this:

2>F:\Code\UnrealEngine\Engine\Source\Runtime\CoreUObject\Public\UObject\Class.h(2829): error C2027: use of undefined type 'AWR2InventoryPhysical'
2>  F:\Code\UnrealEngineProj\Wraith\Source\Wraith\Public\Gameplay2\Characters\Inventory\WR2PickupInventory.h(6): note: see declaration of 'AWR2InventoryPhysical'
2>  F:\Code\UnrealEngine\Engine\Source\Runtime\CoreUObject\Public\UObject\Class.h(2828): note: while compiling class template member function 'UClass *TSubclassOf<AWR2InventoryPhysical>::operator *(void) const'
2>  F:\Code\UnrealEngine\Engine\Source\Runtime\CoreUObject\Public\UObject\ObjectBase.h(1740): note: see reference to function template instantiation 'UClass *TSubclassOf<AWR2InventoryPhysical>::operator *(void) const' being compiled
2>  F:\Code\UnrealEngineProj\Wraith\Source\Wraith\Public\Gameplay2\Characters\Inventory\WR2PickupInventory.h(18): note: see reference to class template instantiation 'TSubclassOf<AWR2InventoryPhysical>' being compiled
2>F:\Code\UnrealEngine\Engine\Source\Runtime\CoreUObject\Public\UObject\Class.h(2829): error C3861: 'StaticClass': identifier not found

Doesn’t matter if I forward declare it at the top of the header file or inline.

The forward declaration is fine. You’re getting an error because somewhere in your code you’re trying to get the UClass pointer (operator*) and that requires the full definition for the type. You need to find the line causing that and ensure you’ve #included the type’s header before that point.

If it’d caused by a line in the same header then you’ll need to move that line to a cpp file if you want to stick with a forward declaration.

I noticed in one case it was a UProperty in a struct that was causing the compiler error.

Another time it was a Key in a TMap.