C++ 调用隐式删除的默认构造函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31168135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 13:52:37  来源:igfitidea点击:

Call to implicitly-deleted default constructor

c++stdarray

提问by Itsbananas

I get the error message Call to implicitly-deleted default constructor of 'std::array'when I try to compile my C++ project.

当我尝试编译我的 C++ 项目时,我收到错误消息Call to 'std::array' 的隐式删除的默认构造函数

Header file cubic_patch.hpp

头文件cubic_patch.hpp

#include <array>
class Point3D{
public:
    Point3D(float, float, float);
private:
    float x,y,z;
};

class CubicPatch{
public:
    CubicPatch(std::array<Point3D, 16>);
    std::array<CubicPatch*, 2> LeftRightSplit(float, float);
    std::array<Point3D, 16> cp;
    CubicPatch *up, *right, *down, *left;
};

Source file cubic_patch.cpp

源文件cubic_patch.cpp

#include "cubic_patch.hpp"
Point3D::Point3D(float x, float y, float z){
    x = x;
    y = y;
    z = z;
}

CubicPatch::CubicPatch(std::array<Point3D, 16> CP){// **Call to implicitly-deleted default constructor of 'std::arraw<Point3D, 16>'**
    cp = CP;
}

std::array<CubicPatch*, 2> CubicPatch::LeftRightSplit(float tLeft, float tRight){
    std::array<CubicPatch*, 2> newpatch;
    /* No code for now. */
    return newpatch;
}

Could someone tell me what is the problem here, please ? I found similar topics but not really the same and I didn't understand the explanations given.

有人能告诉我这里有什么问题吗?我发现了类似的主题,但并不完全相同,而且我不明白给出的解释。

Thanks.

谢谢。

回答by xcvr

Two things. Class members are initialized beforethe body of the constructor, and a default constructor is a constructor with no arguments.

两件事情。类成员在构造函数体之前初始化,默认构造函数是没有参数的构造函数。

Because you didn't tell the compiler how to initialize cp, it tries to call the default constructor for std::array<Point3D, 16>, and there is none, because there is no default constructor for Point3D.

因为你没有告诉编译器如何初始化 cp,它试图调用 的默认构造函数std::array<Point3D, 16>,但没有,因为没有默认的构造函数Point3D

CubicPatch::CubicPatch(std::array<Point3D, 16> CP)
    // cp is attempted to be initialized here!
{
    cp = CP;
}

You can get around this by simply providing an initializer list with your Constructor definition.

您可以通过简单地为您的构造函数定义提供一个初始化列表来解决这个问题。

CubicPatch::CubicPatch(std::array<Point3D, 16> CP)
    : cp(CP)
{}

Also, you might want to have a look at this code.

此外,您可能想看看这段代码。

Point3D::Point3D(float x, float y, float z){
    x = x;
    y = y;
    z = z;
}

x = x, y = y, z = zdoesn't make sense. You're assigning a variable to itself. this->x = xis one option to fix that, but a more c++ style option is to use initializer lists as with cp. They allow you to use the same name for a parameter and a member without the use of this->x = x

x = x, y = y,z = z没有意义。您正在为自身分配一个变量。this->x = x是解决该问题的一种选择,但更多 C++ 风格的选择是使用初始化列表作为cp. 它们允许您对参数和成员使用相同的名称,而无需使用this->x = x

Point3D::Point3D(float x, float y, float z)
    : x(x)
    , y(y)
    , z(z)
{}