UE5.1.1中UFUNCTION宏引发编译失败问题求助
解决UE5 C++暴露函数到蓝图时的编译错误
问题分析
你遇到的编译错误Found 'end of type' when expecting an identifier,核心原因是UE的反射系统(UFUNCTION/UPROPERTY依赖)不支持将标准库的std::string作为蓝图可调用函数的参数类型。UE的蓝图调用体系仅识别自身提供的类型(如FString、FName等),std::string不在反射系统的兼容列表中,导致编译器解析UFUNCTION宏时无法正确识别参数类型,抛出语法错误。
解决方案
将函数参数的std::string替换为UE原生的FString类型,修改后的头文件代码如下:
// Fill out your copyright notice in the Description page of Project Settings. #pragma once #include "CoreMinimal.h" #include "Components/ActorComponent.h" #include "Components/StaticMeshComponent.h" #include "MazeGenerator.generated.h" UCLASS() class MINOTAUR_API UMazeGenerator : public UActorComponent { GENERATED_BODY() public: UPROPERTY(EditAnywhere) int mX; UPROPERTY(EditAnywhere) int mY; // Sets default values for this component's properties UMazeGenerator(); UFUNCTION(BlueprintCallable) void LoadVoxels(FString); // 替换std::string为FString private: UStaticMesh** maze_voxels; };
如果你的函数内部需要使用std::string,可以在函数实现中通过FString的ToStdString()方法完成转换,示例:
void UMazeGenerator::LoadVoxels(FString InString) { std::string StdStr = InString.ToStdString(); // 后续处理std::string的逻辑 }
额外说明
UE的反射系统对类型有严格要求,所有要暴露给蓝图的函数、属性,其类型必须是UE原生类型或标记为UCLASS/USTRUCT的自定义类型,标准库类型(如std::string、std::vector等)无法直接用于UFUNCTION的参数或UPROPERTY的类型。
内容的提问来源于stack exchange,提问作者Stephen Hyberger
相关产品推荐
相关产品推荐

