在cpp文件中,我的使用:
BoxComp->OnComponentBeginOverlap.AddDynamic(this, &AAICharacter::OnBoxOverlap);
BoxComp->OnComponentEndOverlap.AddDynamic(this, &AAICharacter::OnBoxEndOverlap);
得到以下错误:
cannot convert argument 2 from 'void (_cdecl AAICharacter::*)(AActor*,UPrimitiveComponent*,int32,bool,const FHitResult &)' to 'void (_cdecl AAICharacter::*)(UPrimitiveComponent*, AActor*,UPrimitiveComponent*,int32,bool,const FHitResult &)
这是带有相关功能的头文件的外观。省略了一些代码以尝试创建我认为是最小的可复制示例。如果我由于缺乏专业知识而遗漏了任何有价值的东西,请问lmk:
#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "Engine/DataTable.h"
#include "Subtitle.h"
#include "Components/BoxComponent.h"
#include "Components/AudioComponent.h"
#include "AICharacter.generated.h"
/**
* The purpose of this class is to create a dummy AI for testing out the code for character interactions.
*/
UCLASS()
class GV_PROJECT_API AAICharacter : public ACharacter
{
GENERATED_BODY()
public:
// Sets default values for this character's properties
AAICharacter();
private:
UFUNCTION()
void OnBoxOverlap(AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherIndex, bool bFromSweep, const FHitResult & SweepResult);
UFUNCTION()
void OnBoxEndOverlap(AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherIndex)
protected:
/*If the player is inside this box component he will be able to initiate a conversation with the pawn*/
UPROPERTY(VisibleAnywhere)
UBoxComponent* BoxComp;
有人熟悉此错误吗?
错误是不言自明的。
cannot convert argument 2 from 'void (_cdecl AAICharacter::*)(AActor*,UPrimitiveComponent*,int32,bool,const FHitResult &)' to 'void (_cdecl AAICharacter::*)(UPrimitiveComponent*, AActor*,UPrimitiveComponent*,int32,bool,const FHitResult &)
所指示的行中的第二个参数(因此&AAICharacter::OnBoxOverlap
和&AAICharacter::OnBoxEndOverlap
)具有消息中列出的第一种类型,而被调用的函数[AddDynamic()
)则需要第二种类型。有时候,不同的类型不是问题,但是在这种情况下,无法从一种转换为另一种。
我看到的唯一技巧是在比较了所讨论的类型之后。它有助于插入空格,使内容排列得更好。
void (_cdecl AAICharacter::*)( AActor*,UPrimitiveComponent*,int32,bool,const FHitResult &)
void (_cdecl AAICharacter::*)(UPrimitiveComponent*, AActor*,UPrimitiveComponent*,int32,bool,const FHitResult &)
这两个成员函数采用的参数比预期的要少。如果这是唯一使用这些功能的地方,则可以将附加UPrimitiveComponent*
参数直接添加到它们中。否则,您可能想要引入具有所需签名的包装器函数,忽略它们的第一个参数,然后调用该方法(一个包装器将调用OnBoxOverlap()
,而另一个包装器将调用OnBoxEndOverlap()
)。例如:
UFUNCTION()
void OnBoxOverlapWrapper(UPrimitiveComponent* /*ignored*/, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherIndex, bool bFromSweep, const FHitResult & SweepResult)
{
OnBoxOverlap(OtherActor, OtherComp, OtherIndex, bFromSweep, SweepResult);
}
附带警告:您知道您要忽略的参数的重要性吗?您的代码可能包含当前未引起注意的错误,该错误是由于不使用该第一个参数而引起的。