Problem about recursive inclusion

In my project, I need to include my class like below:

Player.h

#include "Tracer.h"
//...

Tracer.h

#include "Loader.h"
//...

Loader.h

#include "Player.h"
//...

Player.cpp

// Add Tracer as a scene component
// Some code here

Tracer.cpp

//Trace Actor By Channel
//part of the code
if (GetWorld()->LineTraceMultiByChannel(HitResultArray, TraceStart, TraceEnd, ECollisionChannel::ECC_GameTraceChannel1, CollisionQueryParams)) {
	for (FHitResult hr : HitResultArray) {
		/*Found Loader*/
		if (hr.Actor->IsA(ALoader::StaticClass())) {
			// Do something
		}
	}
}

Loader.cpp

void Loader::OverlapEnds(UPrimitiveComponent * OverlappedComp, AActor * OtherActor, UPrimitiveComponent * OtherComp, int32 OtherBodyIndex)
{
	if (OtherActor->IsA(APlayer::StaticClass())) {
		//Do something
	}
}

However, this would cause recursive inclusion.

So, I try to add include guards to solve the problem.

But, I get an error “Error: UCLASS inside this preprocessor block will be skipped” if I add the include guards like below:

Tracer.h

#ifndef _LOADER_H_
#define _LOADER_H_ 

#include "Loader.h"

//...

#endif

How should I solve the recursive inclusion problem in UE4?

Hi. You can use interface class to break this include chain. For example make ILoader class with (and only with) virtual function set. You can read more about UObject interfaces here

As result you can get hierarchy like that:

  • ILoader includes nothing

  • Tracer includes ILoader

  • Player includes Tracer includes ILoader

  • Loader includes Player includes Tracer includes ILoader

  • And actual Loader is inheriteded from ILoader interface and implements it