简体   繁体   English

虚幻:遵循教程时,“未知类型名称UTextRenderComponent”编译错误

[英]Unreal: “Unknown type name UTextRenderComponent” compile error while following tutorial

I'm following this tutorial and I literally copied and pasted the content of Countdown.h and Countdown.cpp. 我正在按照本教程进行操作 ,实际上是复制并粘贴Countdown.h和Countdown.cpp的内容。

Visual Studio Code (I'm on mac) gives the following hint Visual Studio Code(我在Mac上)给出以下提示

#include errors detected. Please update your includePath. IntelliSense features for this translation unit (/Users/massimobortolamei/Documents/Unreal Projects/HowTo_VTE/Source/HowTo_VTE/Countdown.h) will be provided by the Tag Parser.
cannot open source file "new" (dependency of "GameFramework/Actor.h")

at this line 在这条线

#include "GameFramework/Actor.h"

while the compilation error I get from UE is the following 而我从UE得到的编译错误如下

Running Mono...

Setting up Mono
~/Documents/UE_4.18/Engine ~/Documents/UE_4.18/Engine/Binaries/Mac
Compiling game modules for hot reload
Parsing headers for HowTo_VTEEditor
  Running UnrealHeaderTool "/Users/massimobortolamei/Documents/Unreal Projects/HowTo_VTE/HowTo_VTE.uproject" "/Users/massimobortolamei/Documents/Unreal Projects/HowTo_VTE/Intermediate/Build/Mac/HowTo_VTEEditor/Development/HowTo_VTEEditor.uhtmanifest" -LogCmds="loginit warning, logexit warning, logdatabase error" -Unattended -WarningsAsErrors -installed
Reflection code generated for HowTo_VTEEditor in 10,277899 seconds
Performing 3 actions (5 in parallel)
[2/3] Compile Countdown.cpp
[1/3] Compile Countdown.gen.cpp
In file included from /Users/massimobortolamei/Documents/Unreal Projects/HowTo_VTE/Intermediate/Build/Mac/UE4Editor/Inc/HowTo_VTE/Countdown.gen.cpp:8:
../../../Unreal Projects/HowTo_VTE/Source/HowTo_VTE/Countdown.h:29:5: error: unknown type name 'UTextRenderComponent'
    UTextRenderComponent* CountdownText;
    ^
../../../Unreal Projects/HowTo_VTE/Source/HowTo_VTE/Countdown.h:12:5: error: cannot initialize return object of type 'UObject *' with an rvalue of type 'ACountdown *'
    GENERATED_BODY()
    ^~~~~~~~~~~~~~~~
Runtime/CoreUObject/Public/UObject/ObjectMacros.h:585:29: note: expanded from macro 'GENERATED_BODY'
#define GENERATED_BODY(...) BODY_MACRO_COMBINE(CURRENT_FILE_ID,_,__LINE__,_GENERATED_BODY);
                            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Runtime/CoreUObject/Public/UObject/ObjectMacros.h:580:37: note: expanded from macro 'BODY_MACRO_COMBINE'
#define BODY_MACRO_COMBINE(A,B,C,D) BODY_MACRO_COMBINE_INNER(A,B,C,D)
                                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Runtime/CoreUObject/Public/UObject/ObjectMacros.h:579:43: note: expanded from macro 'BODY_MACRO_COMBINE_INNER'
#define BODY_MACRO_COMBINE_INNER(A,B,C,D) A##B##C##D
                                          ^~~~~~~~~~
note: (skipping 1 expansions in backtrace; use -fmacro-backtrace-limit=0 to see all)
../../../Unreal Projects/HowTo_VTE/Intermediate/Build/Mac/UE4Editor/Inc/HowTo_VTE/Countdown.generated.h:81:2: note: expanded from macro 'HowTo_VTE_Source_HowTo_VTE_Countdown_h_12_GENERATED_BODY'
        HowTo_VTE_Source_HowTo_VTE_Countdown_h_12_ENHANCED_CONSTRUCTORS \
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../../../Unreal Projects/HowTo_VTE/Intermediate/Build/Mac/UE4Editor/Inc/HowTo_VTE/Countdown.generated.h:57:54: note: expanded from macro 'HowTo_VTE_Source_HowTo_VTE_Countdown_h_12_ENHANCED_CONSTRUCTORS'
        DECLARE_VTABLE_PTR_HELPER_CTOR(NO_API, ACountdown); \
                                                            ^
Runtime/CoreUObject/Public/UObject/ObjectMacros.h:1387:11: note: expanded from macro '\
DEFINE_VTABLE_PTR_HELPER_CTOR_CALLER'
                        return new (EC_InternalUseOnlyConstructor, (UObject*)GetTransientPackage(), NAME_None, RF_NeedLoad | RF_ClassDefaultObject | RF_TagGarbageTemp) TClass(Helper); \
                               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /Users/massimobortolamei/Documents/Unreal Projects/HowTo_VTE/Source/HowTo_VTE/Countdown.cpp:1:
../../../Unreal Projects/HowTo_VTE/Source/HowTo_VTE/Countdown.h:29:5: error: unknown type name 'UTextRenderComponent'
    UTextRenderComponent* CountdownText;
    ^
../../../Unreal Projects/HowTo_VTE/Source/HowTo_VTE/Countdown.h:12:5: error: cannot initialize return object of type 'UObject *' with an rvalue of type 'ACountdown *'
    GENERATED_BODY()
    ^~~~~~~~~~~~~~~~
Runtime/CoreUObject/Public/UObject/ObjectMacros.h:585:29: note: expanded from macro 'GENERATED_BODY'
#define GENERATED_BODY(...) BODY_MACRO_COMBINE(CURRENT_FILE_ID,_,__LINE__,_GENERATED_BODY);
                            ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Runtime/CoreUObject/Public/UObject/ObjectMacros.h:580:37: note: expanded from macro 'BODY_MACRO_COMBINE'
#define BODY_MACRO_COMBINE(A,B,C,D) BODY_MACRO_COMBINE_INNER(A,B,C,D)
                                    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Runtime/CoreUObject/Public/UObject/ObjectMacros.h:579:43: note: expanded from macro 'BODY_MACRO_COMBINE_INNER'
#define BODY_MACRO_COMBINE_INNER(A,B,C,D) A##B##C##D
                                          ^~~~~~~~~~
note: (skipping 1 expansions in backtrace; use -fmacro-backtrace-limit=0 to see all)
../../../Unreal Projects/HowTo_VTE/Intermediate/Build/Mac/UE4Editor/Inc/HowTo_VTE/Countdown.generated.h:81:2: note: expanded from macro 'HowTo_VTE_Source_HowTo_VTE_Countdown_h_12_GENERATED_BODY'
        HowTo_VTE_Source_HowTo_VTE_Countdown_h_12_ENHANCED_CONSTRUCTORS \
        ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
../../../Unreal Projects/HowTo_VTE/Intermediate/Build/Mac/UE4Editor/Inc/HowTo_VTE/Countdown.generated.h:57:54: note: expanded from macro 'HowTo_VTE_Source_HowTo_VTE_Countdown_h_12_ENHANCED_CONSTRUCTORS'
        DECLARE_VTABLE_PTR_HELPER_CTOR(NO_API, ACountdown); \
                                                            ^
Runtime/CoreUObject/Public/UObject/ObjectMacros.h:1387:11: note: expanded from macro '\
DEFINE_VTABLE_PTR_HELPER_CTOR_CALLER'
                        return new (EC_InternalUseOnlyConstructor, (UObject*)GetTransientPackage(), NAME_None, RF_NeedLoad | RF_ClassDefaultObject | RF_TagGarbageTemp) TClass(Helper); \
                               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/Users/massimobortolamei/Documents/Unreal Projects/HowTo_VTE/Source/HowTo_VTE/Countdown.cpp:11:44: error: use of undeclared identifier 'UTextRenderComponent'
    CountdownText = CreateDefaultSubobject<UTextRenderComponent>(TEXT("CountdownNumber"));
                                           ^
/Users/massimobortolamei/Documents/Unreal Projects/HowTo_VTE/Source/HowTo_VTE/Countdown.cpp:12:43: error: use of undeclared identifier 'EHTA_Center'; did you mean 'kTXNCenter'?
    CountdownText->SetHorizontalAlignment(EHTA_Center);
                                          ^~~~~~~~~~~
                                          kTXNCenter
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.13.sdk/System/Library/Frameworks/Carbon.framework/Frameworks/HIToolbox.framework/Headers/MacTextEditor.h:793:3: note: 'kTXNCenter' declared here
  kTXNCenter                    = 4,
  ^
/Users/massimobortolamei/Documents/Unreal Projects/HowTo_VTE/Source/HowTo_VTE/Countdown.cpp:22:12: error: cannot initialize object parameter of type 'AActor' with an expression of type 'ACountdown'
    Super::BeginPlay();
           ^~~~~~~~~
/Users/massimobortolamei/Documents/Unreal Projects/HowTo_VTE/Source/HowTo_VTE/Countdown.cpp:25:5: error: cannot initialize object parameter of type 'const AActor' with an expression of type 'ACountdown'
    GetWorldTimerManager().SetTimer(CountdownTimerHandle, this, &ACountdown::AdvanceTimer, 1.0f, true);
    ^~~~~~~~~~~~~~~~~~~~
/Users/massimobortolamei/Documents/Unreal Projects/HowTo_VTE/Source/HowTo_VTE/Countdown.cpp:31:12: error: no matching member function for call to 'Tick'
    Super::Tick( DeltaTime );
    ~~~~~~~^~~~
Runtime/Engine/Classes/GameFramework/Actor.h:2077:15: note: candidate function not viable: no known conversion from 'ACountdown' to 'AActor' for object argument
        virtual void Tick( float DeltaSeconds );
                     ^
Runtime/Engine/Classes/GameFramework/Actor.h:2432:15: note: candidate function not viable: requires 2 arguments, but 1 was provided
        virtual void Tick( float DeltaTime, enum ELevelTick TickType ) final
                     ^
/Users/massimobortolamei/Documents/Unreal Projects/HowTo_VTE/Source/HowTo_VTE/Countdown.cpp:47:9: error: cannot initialize object parameter of type 'const AActor' with an expression of type 'ACountdown'
        GetWorldTimerManager().ClearTimer(CountdownTimerHandle);
        ^~~~~~~~~~~~~~~~~~~~
2 errors generated.
8 errors generated.
ERROR: UBT ERROR: Failed to produce item: /Users/massimobortolamei/Documents/Unreal Projects/HowTo_VTE/Binaries/Mac/UE4Editor-HowTo_VTE-6063.dylib
Total build time: 28,61 seconds (Local executor: 0,00 seconds)

It's as if the two errors are related, but I don't get what could be wrong. 好像这两个错误是相关的,但我不明白可能有什么错误。 I noticed that while my project is situated in /Documents/Unreal Projects/HowTo_VTE/Source/HowToVTE/Countdown.cpp the Unreal stuff is in Documents/UE_4.18/Engine/... could this be the culprit for which the include is not found? 我注意到,虽然我的项目位于/Documents/Unreal Projects/HowTo_VTE/Source/HowToVTE/Countdown.cpp但虚幻的东西在Documents/UE_4.18/Engine/...这可能是包含的罪魁祸首未找到?

edit: i tried the include with the absolute path to Actor.h but i get the same compile error 编辑:我尝试了包括与Actor.h的绝对路径,但我得到相同的编译错误

I posted this same question on the Answerhub of Unreal, and a kind user was able to provide a solution. 我在虚幻的Answerhub上发布了同样的问题,一个友善的用户能够提供解决方案。

You can see it here 你可以在这里看到

For short: add #include "Runtime/Engine/Classes/Components/TextRenderComponent.h" to the includes in Countdown.h (Leaving the autogenerated include as last, of course) 简而言之:将#include "Runtime/Engine/Classes/Components/TextRenderComponent.h"Countdown.h的包含项中(当然,将自动生成的包含项保留为最后)

Here's how to understand what this error was telling you: 这是了解此错误告诉您的方法:

[1/3] Compile Countdown.gen.cpp
In file included from /Users/massimobortolamei/Documents/Unreal Projects/HowTo_VTE/Intermediate/Build/Mac/UE4Editor/Inc/HowTo_VTE/Countdown.gen.cpp:8:
../../../Unreal Projects/HowTo_VTE/Source/HowTo_VTE/Countdown.h:29:5: error: unknown type name 'UTextRenderComponent'
    UTextRenderComponent* CountdownText;
    ^

Your compiler is telling you that it doesn't know what a UTextRenderComponent is because the file that defines it hasn't yet been included. 您的编译器告诉您它不知道UTextRenderComponent是什么,因为尚未包含定义它的文件。 This is why adding the include ( #include "Runtime/Engine/Classes/Components/TextRenderComponent.h" ) worked. 这就是为什么添加include( #include "Runtime/Engine/Classes/Components/TextRenderComponent.h" )的原因。 It's not, however, generally a good idea to include other headers in your existing headers, as this will bloat your compile times on large projects ( Here's a decent explanation of why this is.) 但是,通常不建议在现有标头中包含其他标头,因为这会在大型项目上浪费您的编译时间( 是为什么这样做的不错解释。)

A better practice is to forward-declare the UTextRenderComponent in your header, and then include the TextRenderComponent header in your .cpp file. 更好的做法是在标头中声明UTextRenderComponent,然后在.cpp文件中包含TextRenderComponent标头。 ( Some info about forward declarations) Unreal's build tool provides a convenient way to do this using the class keyword. (有关前向声明的一些信息 )Unreal的构建工具提供了使用class关键字执行此操作的便捷方法。

Declare your member variable like this: 像这样声明您的成员变量:

class UTextRenderComponent* CountdownText;

And then include the required header in your .cpp file. 然后在.cpp文件中包含所需的标头。 What you're doing in this instance is telling your compiler that UTextRenderComponent is a class, and you're promising to supply the definition later on. 在这种情况下,您要告诉编译器UTextRenderComponent是一个类,并且您保证稍后会提供该定义。 Your compiler doesn't need to know what that definition is while it's parsing Countdown.h because you're just including a pointer to the class, so it doesn't need to know its size or anything else about it - just that it's a class defined somewhere else. 您的编译器在解析Countdown.h时不需要知道该定义是什么,因为您只需要包含指向该类的指针,因此它不需要知道其大小或其他有关它的信息-只是它是一个类在其他地方定义。

So yes, the solution you found above will work but is less ideal than forward-declaring your class in your header and then including other headers in your implementation. 因此,是的,您在上面找到的解决方案可以工作,但比在标头中预先声明您的类然后在实现中包含其他标头的方法不理想。

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

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