2017-05-27 68 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這樣的單體頭文件,但是當您嘗試編譯構建時它會拒絕包含。建立一個使用你現在需要的頭文件的習慣是一個好主意,因爲如果你包含單片頭文件,你將會在後來改變它。 –

相關問題