【问题标题】:AActor *UactorComponent::GetOwner const - pointer to incomplete class type is not allowedAActor *UactorComponent::GetOwner const - 不允许指向不完整类类型的指针
【发布时间】:2017-10-28 10:10:39
【问题描述】:

我是 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 repo 进行了比较,但找不到任何差异。我在虚幻编辑器 4.16.0 上!

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

【问题讨论】:

  • “我错过了什么?” 包含一些包含必要声明的头文件可能吗?
  • 如果它在引擎中编译,但 VS 2017 给我一个错误,我对此表示怀疑。 “也许”-答案不是很有帮助,顺便说一句...
  • 这就是为什么它是评论而不是答案。这肯定是关于缺少声明。我不知道它是什么意思它在引擎中编译
  • 当我在引擎中点击编译时它可以工作。刚刚发现它也可以在 VS 2017 中构建。但我仍然会在编码时得到错误,即使它编译得很好!
  • 这里有同样的问题:/ 在这里链接了问题:github.com/UnrealCourse/03_BuildingEscape/issues/2

标签: c++ unreal-engine4


【解决方案1】:

在 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 进行代码自动补全,它运行速度快,自动补全正确,您不需要包含任何额外的文件。

【讨论】:

    【解决方案2】:

    我想指出,从 Unreal 4.15 开始,包含“Engine.h”与 IWYU 的基本原理背道而驰,因为 Engine.h 很大并且会减慢编译时间。有关此事的更多信息,请参阅Unreal Documentation。因此,尽管它解决了问题,而且这样做并没有什么特别“错误”的地方——它可能不是最好的主意。

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

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

    【讨论】:

    • 你是绝对正确的。我也注意到了减速,而且非常大!但是在开发过程中,拥有它可能比根本没有任何智能感知/自动完成功能而是到处都有错误更好。权衡时间:-)
    • 是的,我可以理解 :) 特别是因为智能感知有时会表现得很奇怪。例如 - GetWorld() 是 UWorld 和 AActor 的一部分,因此在使用 GetWorld() 时包含 GameFramework/Actor.h 或 World.h 将允许编译。但是,如果我包含 World.h,智能感知只能找到 GetWorld()。哦,好吧:P
    • 这在创建打包构建时变得尤为重要。虚幻构建工具会警告您在开发环境中包含像 Engine.h 这样的单片头文件,但当您尝试构建构建时它会拒绝包含。养成使用您现在需要的标头的习惯是个好主意,因为如果您包含整体标头,您以后将不得不更改它。
    猜你喜欢
    • 2021-12-31
    • 2012-08-15
    • 2015-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多