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

2024-04-16

我是 UE4 开发新手,并且学习过 Udemy 的虚幻引擎开发课程。我在 Actor 上创建了一个新组件,名为 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 存储库进行了比较,但找不到任何差异。我使用的是虚幻编辑器 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"

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

NOTE:正如其他帖子中提到的,这个解决方案对于获得智能感知自动完成功能很有帮助,但并不是最好的,因为它会包含大量内容并大大增加编译时间。 包含特定的 .h 文件会更好,但是您需要知道要包含哪些 .h,而且可能您不知道。

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐