Can't recognize some class until include Engine.h

#include “CoreMinimal.h”
#include “GameFramework/Actor.h”
#include “Projectile.generated.h”

UCLASS()
class LEARNINGTANK_API AProjectile : public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
	AProjectile();

protected:
	// Called when the game starts or when spawned
	virtual void BeginPlay() override;

public:	
	// Called every frame
	virtual void Tick(float DeltaTime) override;

private:
	UProjectileMovementComponent* ProjectileMovement = nullptr;

The compiler will complain about that the UProjectMovementComponent is undefined. But after include Engine.h file, the issue is gone. Is that mean I should add Engine.h in each header file? Or it is just a bug of the enging?

Hi, don’t include Engine.h, you will need to add include for everything you want to work with so for the class UProjectileMovementComponent you don’t have to add the include in your header class if you add the Class keyword at the beginning of the declaration like this:

Class UProjectileMovementComponent* ProjectileMovement = nullptr;

However you will need to add include for that particular class you’re working with in your Cpp in this case UProjectileMovementComponent we need this in our Cpp class

#include "GameFramework/ProjectileMovementComponent.h"

This way you only include what you need and it makes it faster to compile.

Thanks for the answer.

Yeah, I’ve also tried this forward declaration but forgot to include that .h file. You remind me this, thanks again.

But I still have a question. when I watched someone do things similar to this in youtube or some where ,they dose not need to worry about this. They just write code like what I post and compile with no problem. Don’t need to include any .h file that comes from the engine. And I remember they were using some old versions like 4.10 . And their header file does not include CoreMinimal.h when created. Is this just a bug? Or a thing that everyone must care about?

I’m a newbee to programming, so sorry about this kind of questions.

You’re welcome, if I remember correctly they do have the ProjectName.h included in all their classes, and that has the Engine.h included in it I believe. Only recently UE4 changed to a Include-What-You-Use (IWYU) system, we indeed used to work with Engine.h and go from there but for performance purposes this system was applied recently. You can check it out here:

https://docs.unrealengine.com/en-us/Programming/UnrealBuildSystem/IWYUReferenceGuide

Ah, that‘s exactly what I need to know. Thank you very much!

You’re welcome, please mark this as answered so others can find it easier.