c++ - 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;
}
};
解决方案
您在 FcraftingRecipie 中的第 19 行声明了一个 FMasterItem 字段 (itemClass)。但是,您只能在同一文件中随后声明 FMasterItem。这在 C++ 中不起作用,您需要先定义类,然后才能在同一文件中的其他位置引用它(这就是错误说类型无法识别的原因)。
我建议将 FMasterItem 下的 FcraftingRecipie 声明移动。然而 FMasterItem 也引用了 FcraftingRecipie。您创建的是循环依赖,即 A 需要知道 B,但 B 需要知道 A。这种体面可以通过使用引用/指针而不是值类型字段来解决。
推荐阅读
- r - 自定义 r beeswarm 图
- c++ - 参数 1 没有已知的转换 - 将“this”传递给方法
- git - 无法签署我的提交(Windows 上的 GPG 密钥)
- pygame - 带有级别选择的 Pygame 按钮错误
- python - 链接 GET 和 POST 请求不好吗?
- huggingface-transformers - “ValueError:您必须在使用 Trainer 时指定 input_ids 或 inputs_embeds”
- python - 从密码生成 RSA 公钥和私钥对
- class - 我正在学习 kotlin,我观察到当我们通过点运算符访问主函数中的类的属性时,它不会显示任何错误
- javascript - 从控制台日志中用 Javascript 构建直方图?
- angular - 如何为 Bigcommerce API 修复 Angular 中的 CORS 错误