【问题标题】:OnComponentBeginOverlap.AddDynamic says no instance of the function template matches the argument list?OnComponentBeginOverlap.AddDynamic 说没有函数模板的实例与参数列表匹配?
【发布时间】:2020-06-08 04:37:57
【问题描述】:

在一个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;

有人知道这个错误吗?

【问题讨论】:

  • 至于错误信息,它是相当不言自明的。当您需要类型为void (_cdecl AAICharacter::*)(UPrimitiveComponent*, AActor*,UPrimitiveComponent*,int32,bool,const FHitResult &) 的东西时,作为第二个参数提供的东西是void (_cdecl AAICharacter::*)(AActor*,UPrimitiveComponent*,int32,bool,const FHitResult &) 类型。 (如果您看不到区别,请将这两种类型放在文本编辑器中的连续行中。)
  • A minimal reproducible example 将删除 AAICharacter 中不需要重现错误的部分。除了OnBoxOverlapOnBoxEndOverlap 的声明之外,你还需要从那个类中获得多少?
  • @JaMiT 我确实理解错误在说什么,但我不确定如何为程序提供它想要的东西。我尝试在它应该缺少的地方添加一个原始组件,但所做的只是创建了一大堆新错误。
  • 至于重现错误不需要的部分,老实说我不知道​​。我对使用虚幻引擎的 c++ 非常非常,我担心尝试修剪某些东西可能会删除可以帮助某人解决问题的有价值的信息。不过回想起来,我想我可以安全地省略 ufunctions 和 upproperties,所以我会编辑这些部分。
  • 通常不知道重现错误需要哪些部分,哪些不需要。这就是为什么标准程序是复制您的代码,并在复制中开始剥离内容。如果错误消失了,你已经剥离了太多。如果没有其他内容可以删除而不删除错误,则示例很少。

标签: c++ unreal-engine4


【解决方案1】:

这个错误是不言自明的。

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);
}

这有一个警告:你知道你忽略的参数的重要性吗?您的代码可能包含一个当前未被注意到的错误,该错误源于未使用第一个参数。

【讨论】:

  • 此解决方案与 OnBoxEndOverlap 函数配合良好,但 OnBoxOverlap 函数的版本在以下行出现“缺少变量名”的错误:UFUNCTION() void OnBoxOverlapWrapper(UPrimitiveComponent* /*ignored*/, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherIndex, bool bFromSweep, const FHitResult & SweepResult)
  • @DualBall 没有足够的信息来弄清楚发生了什么。另外,这似乎是一个新问题。
  • 明白了;我会在别处问我的新问题。感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-09-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多