C++ sub-classes not inheriting includes from parent

Hi,

I have been working in the same Visual Studio project for a couple months, and just recently every time I change a file it returns compile errors like those below:

Error C2027 use of undefined type ‘UWorld’

Error C2027 use of undefined type ‘APlayerController’

Error C2061 syntax error: identifier ‘UMaterial’

It can be fixed by manually by re-including everything, but I have never had to do this before and every class that I modify now starts throwing these errors. I think it may be related to the dependencies in Visual Studio, because I started programming in OpenGL and messed with the environment.

Below is an example of a class that compiles in an empty project but not my own. (I know that this can be directly fixed by adding the ‘SkeletalMeshComponent.h’ include in the source file and forward declaring ‘class SkeletalMeshComponent’ in the header)

#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "CustomActor.generated.h"

UCLASS()
class TEST_API ACustomActor : public AActor
{
	GENERATED_BODY()
	
public:	
	ACustomActor();

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components")
		USkeletalMeshComponent* MeshComp;
};

//CPP
#include "CustomActor.h"

ACustomActor::ACustomActor()
{
	MeshComp = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("MeshComp"));
}

Please let me know if you have any insight - thanks,

Kevin

Do these errors occur when you compile in both Visual Studio and in the editor?

Compiling in the editor gives the same errors, so I don’t think it’s Intellisense.

Update: Luckily not every newly edited source file throws missing includes, but I have noticed now that updates to the scene in the Debugger (DebugGameEditor) do not show up in the UProject which may be related.

Have you tried regenerate the VS project file? (Project Folder-> Right Click at .uproject → Generate VS Project File)

Also what do you mean by “re-including everything” ? You mean you have to add like #include "World.h" on each .cpp?

Thank you, I have tried regenerating the VS project files after removing the Binaries and Intermediate folder and reverting to a previous commit, but the issue still remained.

As an example, I have a pawn-derived class with typical attachments like AttachObjectComp->SetupAttachment(PawnMesh); but it started throwing errors like cannot convert argument 1 from ‘USkeletalMeshComponent *’ to ‘USceneComponent *’.

Correct, by “re-including” I mean solving the above case by including StaticMeshComponent.h and SkeletalMeshComponent.h which never had needed to be included before.

Can you post one of the file in question? It’s hard to troubleshoot without seeing your included files.

Can you reproduce the issue in a new blank C++ project?

The laziest fix is probably to include EngineMinimal.h in the your .cpp files but I won’t recommend it.

If your includes are in the header, when you include the sub-class header it would inherit. However, that’s not the right way to do it. You might have class declarations in the header and then includes in the CPP file.

The headers themselves need to be included in the new CPP file. It’s a pain but it’s the correct and really the only way to do this. It means you have to include them every time but then you can only include what you need as opposed to everything every time.

Including EngineMinimal.h or the specific Engine components needed by the class does always fix it, but you’re right: this is something I want to avoid.

I cannot replicate this in a new project, which is why I do think it’s something to do with configuration/linking.

Below is an example of a use case that compiles in an empty project but not my own. (Throws “use of undefined type “USkeletalMeshComponent”” and ‘=’ cannot convert from ‘USkeletalMeshComponent*’ to ‘USceneComponent *’)

#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "CustomActor.generated.h"
UCLASS()
class TEST_API ACustomActor : public AActor
{
	GENERATED_BODY()
public:	
	ACustomActor();

	UPROPERTY(VisibleAnywhere, BlueprintReadOnly, Category = "Components")
		USkeletalMeshComponent* MeshComp;
};

//CPP
ACustomActor::ACustomActor()
{
	MeshComp = CreateDefaultSubobject<USkeletalMeshComponent>(TEXT("MeshComp"));
	if (MeshComp) {
		RootComponent = MeshComp;
	}

}

// Derived Class:
#pragma once
#include "CoreMinimal.h"
#include "CustomActor.h"
#include "CustomActor_DerivedClass.generated.h"
UCLASS()
class TEST_API ACustomActor_DerivedClass : public ACustomActor
{
	GENERATED_BODY()
protected:
	void TestFunction();
};

// CPP
#include "CustomActor_DerivedClass.h"
void ACustomActor_DerivedClass::TestFunction() {
	FVector Location = MeshComp->GetSocketLocation(NAME_None);
}

Thank you BeeGeeDee, for clarification, I have been following the standard conventions and not putting includes in the header (forward declaring non standard engine types in the header file and including appropriate headers in the CPP file).

I have responded to madturtle84 with an example of a case that illustrates my particular issue.

Not a problem, to clarify: the above code does compile in an empty project with or without the forward declaration of USkeletalMeshComponent in CustomActor.h.

You are completely correct that forward declaring “class USkeletalMeshComponent” in the header & including SkeletalMeshComponent.h in the source solves the issue in the general sense.

My concern is that these standard engine types have never needed to be included for me in inherited classes.

I have updated the question with a more succinct example.

I see the code. If you add the missing headers into the CPP, it should just work, I believe the convention is they would go just above the code at line 40.

Hi, sorry, I see the issue.
There are 2 fixes you can do and it’s down to the user preference.

  1. Add class in front of the MeshComp declaration i.e. class USkeletalMeshComponent* MeshComp
  2. just above the class, between lines 4 and 5, add class USkeletalMeshComponent;

Either works and if you are using multiple variables from the same class, number 2 can be neater.

You will still need to add the include in your .cpp file as well to use the MeshComp member variable. Include the following:

#include "Components/SkeletalMeshComponent.h"

Generally it would work but it’s only when you start needing to use them it complains, like for accessing member methods and variables. Then you have to add the header in. Still gets me every time wondering why intellisense isn’t working.

OK I think this is the reason: https://docs.unrealengine.com/en-us/Programming/UnrealBuildSystem/IWYUReferenceGuide

Look for [ProjName].Build.cs and compare it with a new project.

TLDR: Old projects include PCH that contain most gameplay classes. Starting from 4.15 you need to include them individually. This of course make compilation faster.

Thank you! PCH settings were entirely the problem.

I started the project in 4.20, so the Build.cs files were the same for my project and the test, but I read into the documentation you linked and figured out that having git in the project messed with adaptive unity build settings.

I.e.: Every time I changed a file in source control, PCH would be disabled for those files.

Solution (in BuildConfiguration.xml):

<?xml version="1.0" encoding="utf-8" ?>
<Configuration xmlns="https://www.unrealengine.com/BuildConfiguration">
  <BuildConfiguration>
  <bAdaptiveUnityDisablesPCH>false</bAdaptiveUnityDisablesPCH>
  </BuildConfiguration>
</Configuration>