AActor *UactorComponent::GetOwner const - pointer to incomplete class type is not allowed

做~自己de王妃 提交于 2020-05-26 09:07:26

问题


I'm new to UE4 development and I've followed Udemy's Unreal Engine Development course. I have created a new Component on an Actor, named PositionReporter with header PositionReporter.h

#pragma once

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


UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class BUILDINGESCAPE_API UPositionReporter : public UActorComponent
{
    GENERATED_BODY()

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

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

public: 
    // Called every frame
    virtual void TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction) override;
};

and code in PositionReporter.cpp being

#include "PositionReporter.h"

// Sets default values for this component's properties
UPositionReporter::UPositionReporter()
{
    // Set this component to be initialized when the game starts, and to be ticked every frame.  You can turn these features
    // off to improve performance if you don't need them.
    PrimaryComponentTick.bCanEverTick = true;

    // ...
}


// Called when the game starts
void UPositionReporter::BeginPlay()
{
    Super::BeginPlay();

    FString t = GetOwner()->GetActorLabel();

    UE_LOG(LogTemp, Warning, TEXT("Position Reporter reporting for duty on %s"), *t);

}


// Called every frame
void UPositionReporter::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
    Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

    // ...
}

As you can see, I am now trying to call the GetName function on the Pointer to the AActor retrieved through GetObject().

However, as soon as I type "GetObject()->" no autocomplete pops up (as it does in the video) and when I add "GetName()" manually, I get the compiler error "pointer to incomplete class type is not allowed".

What am doing wrong? Am I missing an import or so? I already compared my code to Ben's git repo but can't find any differences. I am on unreal editor 4.16.0!

I noticed another strange thing: When I compile everything from Unreal Engine Editor, it compiles and runs fine. But when I compile it with VS 2017 I get the error, and I also dont get the Autocomplete, which is a real bummer. What am I missing?


回答1:


Including Engine.h on PositionReporter.h fixes the issue.

#pragma once

#include "Engine.h" // <- This
#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "PositionReporter.generated.h"

You'll need to give Intellisense some time... As a matter of fact I closed and reopened the solution for it to stop showing the non existing errors and give autocomplete functionality.

NOTE: As is mentioned in other posts, this solution is good to get intellisense autocompleting but isn't the best as it will include a ton of stuff and greatly increase compilation times. Including the specific .h file would be better, but you need to know what .h to include, and probably you don't know it.

Best solution I found is ditching Intellisense and use Resharper for code autocompletion, it works fast, autocompletes correctly and you don't need to include any extra file.




回答2:


I would like to point out that including "Engine.h" is going against the IWYU rationale from Unreal 4.15 onwards, because Engine.h is huge and slows down compile time. See the Unreal Documentation for more information on the matter. So although it solves the problem, and there is nothing particularly 'wrong' with doing it - it may not be the best idea.

Another option which is more in line with IWYU would be to include Actor.h in PositionReport.cpp since GetOwner is in the AActor class.

#include "PositionReport.h"
#include "GameFramework/Actor.h" //This is the change in PositionReport.cpp


来源:https://stackoverflow.com/questions/44219810/aactor-uactorcomponentgetowner-const-pointer-to-incomplete-class-type-is-no

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!