繁体   English   中英

AActor * UactorComponent :: GetOwner const - 不允许指向不完整类类型的指针

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

我是UE4开发的新手,我遵循了Udemy的虚幻引擎开发课程。 我在Actor上创建了一个新的Component,名为PositionReporter,标题为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;
};

以及PositionReporter.cpp中的代码

#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);

    // ...
}

如您所见,我现在尝试通过GetObject()检索指向AActor的指针上的GetName函数。

但是,只要我输入“GetObject() - >”弹出没有自动完成(就像在视频中那样),当我手动添加“GetName()”时,我得到编译器错误“指向不完整的类类型的指针不是允许”。

怎么了? 我错过了进口吗? 我已经将我的代码与Ben的git repo进行了比较,但是找不到任何差异。 我在虚幻编辑器4.16.0上!

我注意到另一个奇怪的事情:当我从虚幻引擎编辑器编译所有内容时,它编译并运行正常。 但是当我用VS 2017编译它时,我得到了错误,而且我也没有得到自动完成,这真是一个真正的无赖。 我错过了什么?

在PositionReporter.h上包含Engine.h可以解决这个问题。

#pragma once

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

你需要给Intellisense一些时间......事实上,我关闭并重新打开它的解决方案,以便停止显示不存在的错误并提供自动完成功能。

注意:正如其他帖子中提到的,这种解决方案很适合智能感知自动完成,但不是最好的,因为它将包含大量的东西并大大增加编译时间。 包括特定的.h文件会更好,但你需要知道要包含什么.h,可能你不知道。

我找到的最佳解决方案是放弃Intellisense并使用Resharper进行代码自动完成,它运行速度快,自动填充正确,您不需要包含任何额外的文件。

我想指出,包括“Engine.h”违反了虚幻4.15以后的IWYU基本原理,因为Engine.h非常庞大并且减慢了编译时间。 有关此问题的更多信息,请参阅虚幻文档 因此,尽管它解决了这个问题,并且没有做任何特别“错误”的事情 - 但这可能不是最好的主意。

另一个更符合IWYU的选择是在PositionReport.cpp中包含Actor.h,因为GetOwner在AActor类中。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM