构造函数要求参数个数错误

Constructor Asking for Wrong Number of Arguments

我在 Unreal 中构建了一个名为 Groundmode 的 class

GroundMode.h: #pragma once

#include "CoreMinimal.h"
#include "UObject/NoExportTypes.h"
#include "Components/BoxComponent.h"
#include <NMR\GroundDirection.h>
#include "GroundMode.generated.h"

UCLASS()
class NMR_API UGroundMode : public UObject
{
    GENERATED_BODY()

    public:
        UGroundMode();   
};

GroundMode.cpp:

#include "GroundMode.h"

UGroundMode::UGroundMode() {}

我希望能够在另一个 class 中实例化此 class。 PlayerPhysicsCollisionInfo.h

#include "CoreMinimal.h"
#include "Components/ActorComponent.h"
#include "GroundMode.h"
#include "PlayerPhysicsCollisionInfo.generated.h"


UCLASS( ClassGroup=(Custom), meta=(BlueprintSpawnableComponent) )
class NMR_API UPlayerPhysicsCollisionInfo : public UActorComponent
{
    GENERATED_BODY()

public: 
    UPlayerPhysicsCollisionInfo();

    UGroundMode downMode;
    UGroundMode upMode;
    UGroundMode leftMode;
    UGroundMode rightMode;
};

PlayerPhysicsCollisionInfo.cpp: #include "PlayerPhysicsCollisionInfo.h"

    void UPlayerPhysicsCollisionInfo::BeginPlay()
    {
        Super::BeginPlay();
        downMode = new UGroundMode();
        upMode = new UGroundMode();
        leftMode = new UGroundMode();
        rightMode = new UGroundMode();
    }

当我尝试使用 new 关键字创建 UGroundMode 实例时,出现以下编译错误: D:\Documents\Unreal Projects\NMR\Source\NMR\PlayerPhysicsCollisionInfo.cpp(25) : error C2661: 'UGroundMode::operator new': 没有重载函数接受 1 个参数

我做错了什么?构造函数不接受不为 1 的参数。

new 表达式,就像你在这里所做的那样,做了两件事。

  1. 它通过为该对象调用适当的分配函数来为该对象分配内存。
  2. 它在分配的内存中构造对象。

当它调用 class T 的分配函数时,它首先确定 class、std::size_t size 的大小,并将其作为第一个参数传递到分配函数。 class 的分配函数是其范围内 class 可用的第一个解析的 operator new() 函数。然后将任何可选的 放置参数 作为下一个参数发送到分配函数。放置参数是紧跟在 new 关键字之后的括号括起来的表达式:

T* foo = new T(); // good old fashioned uncomplicated new
          ^
          |
          +---- wants to call a `operator new(std::size_t)` function

T* bar = new(1, 2) T(); // a little more complicated
          ^    ^
          |    |
          |    +----- these are placement params
          +---------- wants to call `operator new(std::size_t, int, int)`

您的问题是当 new 表达式尝试进行分配并找到合适的 operator new() 时。 类 可以覆盖 operator new(),而您的 class 可以。 new 关键字将采用 sizeof(T),并尝试将其作为第一个参数发送到 class 定义的 operator new 函数。

你问 class 定义的重载 operator new() 函数在哪里?

Right here

UObject,您的 class 继承自,有一个 void* UObject::operator new() 声明的函数需要五个参数,算上五个参数。

new 关键字将此 operator new() 标识为您的 class' operator new(),但发现您只传递了一个参数(sizeof(T) ),但失败了。


解决方法

如链接的 Unreal 页面所述,new 表达式不能与 UObject 对象一起使用。请改用 StaticConstructObject()


如果你想调用全局operator new无论如何

如果你想让 Unreal engine 甚至 unrealer,你可以通过显式限定函数的范围来指定适当的 operator new 函数。您可以 尝试 访问全局 operator new() 函数,方法是将全局范围指示符放在您的前面 new 语句:

downMode = ::new UGroundMode();

当然,然后你会 运行 进入 ::new UGroundMode() 的问题,就像任何其他 new 操作一样,returns 一个 UGroundMode* 你正在尝试存储在 UGroundMode 对象中。这将失败。我不知道 Unreal engine 会做什么。可能是坏事。


进一步阅读

https://en.cppreference.com/w/cpp/language/new
https://en.cppreference.com/w/cpp/memory/new/operator_new