【问题标题】:Unreal: "Unknown type name UTextRenderComponent" compile error while following tutorial虚幻:“未知类型名称 UTextRenderComponent”在遵循教程时编译错误
【发布时间】:2018-05-14 16:52:32
【问题描述】:

我正在关注this tutorial,我确实复制并粘贴了 Countdown.h 和 Countdown.cpp 的内容。

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")

在这一行

#include "GameFramework/Actor.h"

虽然我从 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)

好像这两个错误是相关的,但我不明白可能出了什么问题。 我注意到虽然我的项目位于/Documents/Unreal Projects/HowTo_VTE/Source/HowToVTE/Countdown.cpp 虚幻的东西在Documents/UE_4.18/Engine/... 这可能是找不到包含的罪魁祸首吗?

编辑:我尝试了包含 Actor.h 的绝对路径的包含,但我得到了相同的编译错误

【问题讨论】:

    标签: c++ compiler-errors unreal-engine4


    【解决方案1】:

    以下是如何理解此错误告诉您的内容:

    [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;
        ^
    

    您的编译器告诉您它不知道 UTextRenderComponent 是什么,因为尚未包含定义它的文件。这就是添加包含 (#include "Runtime/Engine/Classes/Components/TextRenderComponent.h") 有效的原因。但是,在现有头文件中包含其他头文件通常不是一个好主意,因为这会增加您在大型项目中的编译时间(Here's 很好地解释了为什么会这样。)

    更好的做法是在您的标头中前向声明 UTextRenderComponent,然后在您的 .cpp 文件中包含 TextRenderComponent 标头。 (Some info 关于前向声明)虚幻的构建工具使用 class 关键字提供了一种方便的方法。

    像这样声明你的成员变量:

    class UTextRenderComponent* CountdownText;
    

    然后在您的 .cpp 文件中包含所需的标头。 在这种情况下,您所做的是告诉编译器 UTextRenderComponent 是一个类,并且您承诺稍后会提供定义。您的编译器在解析 Countdown.h 时不需要知道该定义是什么,因为您只包含指向该类的指针,因此它不需要知道其大小或有关它的任何其他信息 - 只是它是一个类在其他地方定义。

    所以,是的,您在上面找到的解决方案会起作用,但不如在您的标头中前向声明您的类,然后在您的实现中包含其他标头那么理想。

    【讨论】:

    • 感谢您抽出宝贵时间分享您的见解。我阅读了有关为什么编译时间会受到在其他头文件中声明头文件的影响的链接,并且我还阅读了第一条评论,该评论指出这并不总是一个好主意,因为它确实取决于。在这种特定情况下,Countdown.h 仅由 Countdown.cpp 使用,在 Countdown.h 或 Countdown.cpp 中声明缺少的 TextRenderComponent.h 是否相同?
    • 在这种情况下,是的,您可能会在 Countdown 标题中包含标题,因为标题不包含在其他地方。如果您在标头中使用用户定义的类或结构,有时在标头中包含另一个标头也是不可避免的。不过,总的来说,最好养成使用前向声明的习惯,以便尽可能在标头中包含标头,因为这将对更大的项目产生影响。不过,您的直觉是正确的 - 该指南在特定情况下是否有意义取决于。
    【解决方案2】:

    我在 Unreal 的 Answerhub 上发布了同样的问题,一位好心的用户能够提供解决方案。

    你可以看到here

    简而言之:将 #include "Runtime/Engine/Classes/Components/TextRenderComponent.h" 添加到 Countdown.h 中的包含(当然,将自动生成的包含保留在最后)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-17
      • 1970-01-01
      • 2022-10-31
      • 1970-01-01
      • 1970-01-01
      • 2018-06-10
      • 2014-12-12
      • 2015-10-20
      相关资源
      最近更新 更多