首页 > 解决方案 > UE4 C++ 无法识别的类型“FMasterItem” - 类型必须是 UCLASS、USTRUCT 或 UENUM

问题描述

我在声明 FcraftingRecipie 结构中的变量时遇到问题。由于第 19 行抛出错误无法识别的类型'FMasterItem' - 类型必须是 UCLASS、USTRUCT 或 UENUM - MasterItem.h - 第 19 行

主项目需要存储一个 FCraftingRecipie 数组,因为这是所需项目和数量的列表。

这是 MasterItem 头文件的副本

#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "Engine/DataTable.h"
#include "MasterItem.generated.h"

USTRUCT(BlueprintType)
struct FCraftingRecipie : public FTableRowBase
{
    GENERATED_BODY()

public:
    FCraftingRecipie();

    UPROPERTY(EditAnywhere, BlueprintReadWrite)
        FMasterItem itemClass;

    UPROPERTY(EditAnywhere, BlueprintReadWrite)
        int amount;
};

USTRUCT(BlueprintType)
struct FMasterItem : public FTableRowBase
{
    GENERATED_BODY()

public:
    FMasterItem();

    UPROPERTY(EditAnywhere, BlueprintReadWrite)
        FName itemID;

    UPROPERTY(EditAnywhere, BlueprintReadWrite)
        FText name;

    UPROPERTY(EditAnywhere, BlueprintReadWrite)
        FText description;

    UPROPERTY(EditAnywhere, BlueprintReadWrite)
        UTexture2D* itemIcon;

    UPROPERTY(EditAnywhere, BlueprintReadWrite)
        bool canBeUsed;

    UPROPERTY(EditAnywhere, BlueprintReadWrite)
        FText useText;

    UPROPERTY(EditAnywhere, BlueprintReadWrite)
        bool isStackable;

    UPROPERTY(EditAnywhere, BlueprintReadWrite)
        float itemWeight;

    UPROPERTY(EditAnywhere, BlueprintReadWrite)
        TArray<FCraftingRecipie> recipie;

    UPROPERTY(EditAnywhere, BlueprintReadWrite)
        float price;

    bool operator==(const FMasterItem& OtherItem) const
    {
        if (itemID == OtherItem.itemID)
        {
            return true;
        }

        return false;
    }
};

标签: c++structureunreal-engine4

解决方案


您在 FcraftingRecipie 中的第 19 行声明了一个 FMasterItem 字段 (itemClass)。但是,您只能在同一文件中随后声明 FMasterItem。这在 C++ 中不起作用,您需要先定义类,然后才能在同一文件中的其他位置引用它(这就是错误说类型无法识别的原因)。

我建议将 FMasterItem 下的 FcraftingRecipie 声明移动。然而 FMasterItem 也引用了 FcraftingRecipie。您创建的是循环依赖,即 A 需要知道 B,但 B 需要知道 A。这种体面可以通过使用引用/指针而不是值类型字段来解决。


推荐阅读