2017-05-27 71 views
1

我是UE4开发新手,我遵循Udemy的虚幻引擎开发课程。我已经创建上演员的新组件,在PositionReporter.cpp命名PositionReporter与头PositionReporter.hAActor * UactorComponent :: GetOwner const - 不允许指向不完整的类类型的指针

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

和代码是

#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进行编译时,出现错误,而且我也没有得到Autocomplete,这是一个真正的失望。我错过了什么?

+0

_“我缺少什么?”_要包含一些包含必要声明的头文件,也许? –

+0

如果它在引擎中编译,但VS 2017给我一个错误,我怀疑这一点。 “也许”-answers不是很有帮助,btw ... – Christian

+0

这就是为什么它是一个评论,而不是一个答案。这是关于错过声明。我不知道它在engine_中编译的意思。 –

回答

3

在PositionReporter.h上包含Engine.h修复了这个问题。

#pragma once 

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

你需要给智能感知一段时间......作为事实上我关闭并重新打开解决方案,它停止显示不存在的错误,并给自动完成功能。

注:正如在其他帖子中提到,该解决方案是很好的获得智能自动填充,但不是最好的,因为它会包括一吨的东西,并大大增加编译时间。 包含特定的.h文件会更好,但您需要知道要包含什么.h,并且可能您不知道它。

我发现最好的解决方案是沟通Intellisense并使用Resharper进行代码自动完成,它工作的很快,自动完成正确,并且不需要包含任何额外的文件。

3

我想指出的是,包含“Engine.h”与虚幻4.15之后的IWYU原理是背道而驰的,因为Engine.h非常庞大并且会缩短编译时间。有关此事的更多信息,请参阅Unreal Documentation。所以,尽管它解决了这个问题,而且这样做没有什么特别的错误 - 但它可能不是最好的主意。

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

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

你是绝对正确的。我注意到放慢速度也很快,而且很大!但是在开发过程中,与没有任何智能感知/自动完成功能相比,这样做可能会更好,而且会在整个地方出现错误。折衷时间:-) – Christian

+0

是的,我可以理解:)特别是因为intellisense有时会表现怪异。例如,GetWorld()是UWorld和AActor的一部分,因此包括GameFramework/Actor.h或World.h在使用GetWorld()时将允许编译。但是,如果我包含World.h,intellisense只能找到GetWorld()。哦:P – flabby99

+1

创建打包版本时,这变得尤为重要。 Unreal构建工具会警告您在开发环境中包含一个像Engine.h这样的单体头文件,但是当您尝试编译构建时它会拒绝包含。建立一个使用你现在需要的头文件的习惯是一个好主意,因为如果你包含单片头文件,你将会在后来改变它。 –

相关问题