Pointer to incomplete class type not allowed

Hey everyone!

I’m pretty new to C++ programming in UE4 and I got the following problem:
I have created the ActorComponent “PositionOwner” and I would like to read out the object’s owner by:

void UPositionOwner::BeginPlay()
{
	Super::BeginPlay();
	FString ObjectName = GetOwner()->GetName();
}

Visual Studio marks “GetOwner()” with this error: “pointer to incomplete class type is not allowed”. I googled and found that:

but unfortunately this is not helping me enough. I tried to several #includes of AActor but nothing works.

What exactly do I have to search for and #include to use this function “GetOwner”?

Thanks in advance!
Kind regards

I’m pretty new myself but, what class are you calling this from and does it inherit from AActor? Like so:

UCLASS()
class MYPROJECT_API AMyClass: public AActor
{
	GENERATED_BODY()
	
public:	
	// Sets default values for this actor's properties
.
.
.
}

I’m kinda guessing it doesn’t because of the “UPositionOwner::” If your class was an actor it would probably be “APositionOwner”

I tried grabbing your line of code:
FString ObjectName = GetOwner()->GetName();
and dropping it into one of my Actors and there’s no problem there.

My class inherits from UActorComponent:

#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "PositionOwner.generated.h"


UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class PROJECT_API UPositionOwner : public UActorComponent
{
	GENERATED_BODY()

public:	
	// Sets default values for this component's properties
	UPositionOwner();

protected:
	// Called when the game starts
	virtual void BeginPlay() override;
... };

Hey Nebo,

Can you try? :

#include "GameFramework/Actor.h"

void UPositionOwner::BeginPlay()
 {
     Super::BeginPlay();
     FString ObjectName = GetOwner()->GetName();
 }

Perfect! That works for me - thank you very much :slight_smile:
But how did you know what to #include into the .cpp? I’m asking to be prepared for future errors.

This is sort of a tricky one to figure out because it’s not so obvious what the problem is. Anytime you are calling a function that returns a type, such as GetOwner( ) returning AActor, you should make sure that you have included the header file that corresponds with that type. This is especially true when you get the 'Pointer to incomplete class" error, as that means the compiler doesn’t know what the type is for the pointer being used: “GetOwner()->”

The problem is that GetOwner( ) returns a Actor type but the UPositionOwner cpp file doesn’t know what that type is, so you have to include the Actor.h file.

If you search “UActorComponent” on the documentation site, it leads you to:

[UActorComponent][1]

At that page, you can then search for the function: GetOwner( )

AActor * GetOwner() Follow the Outer chain to get the AActor that ‘Owns’ this component

You can see that it returns the type of AActor. That is how I knew that you needed to included Actor.h.

Thank you for your explanation - brings me little closer to C++ :slight_smile: