I’m stumped by a UE4 mystery, and hoping for help. It seems my project has lost the capacity to handle forward declarations in conjunction with TSubclassOf variable declarations.
What I wanted was simple. I had a project with a UObject-derived class in which I wanted to create a variable which I could, through blueprints, set equal to a particular blueprint class. To avoid circular dependencies, I don’t want to #include the class of the object in question. Forward declarations are the answer! But they don’t work.
After a lot of frustration, I created a new project (TestProj) and tried something out. I created two new classes: Porkchop and HamSalad, both subclasses of Object. Here is Porkchop.h:
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "UObject/NoExportTypes.h"
#include "Porkchop.generated.h"
/**
*
*/
UCLASS()
class TESTPROJ_API UPorkchop : public UObject
{
GENERATED_BODY()
public:
};
And here is HamSalad.h:
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "UObject/NoExportTypes.h"
#include "HamSalad.generated.h"
/**
*
*/
UCLASS()
class TESTPROJ_API UHamSalad : public UObject
{
GENERATED_BODY()
TSubclassOf<class UPorkchop> MainCourse;
};
Visual Studio 2017 is not happy with this; MainCourse has the squiggly red underline, with the tooltip “Incomplete type is not allowed”. Nevertheless, it compiles.
Now here’s the part that has me pulling my hair out: if I add these two classes, Porkchop and HamSalad, both derived from UObject and exactly as described above, to MY project, it does not compile. I get this in Visual Studio’s output:
F:\UE4\Oafmatch\Source\Oafmatch/HamSalad.h(17): error C2079: 'UHamSalad::MainCourse' uses undefined class 'TSubclassOf<UPorkchop>'
More weirdness: if I add the line
TSubclassOf<class UPorkchop> MainCourse;
to the body of any AActor-derived class in my project, it compiles. But not in any UObject-derived class.
What on earth is going on here?