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

香蕉

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

头文件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;
};

源文件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;
}

有人可以告诉我这是什么问题吗?我发现了类似的主题,但并非完全相同,而且我不理解给出的解释。

谢谢。

xcvr

两件事情。类成员在构造函数的主体之前初始化,并且默认构造函数是不带参数的构造函数。

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

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

您只需提供带有构造函数定义的初始化列表即可解决此问题。

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

另外,您可能想看一下这段代码。

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

x = xy = yz = z没有任何意义。您正在为其分配一个变量。this->x = x是解决此问题的一种方法,但更多的c ++样式选项是使用与一样的初始化列表cp它们允许您对参数和成员使用相同的名称,而无需使用this->x = x

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

隐式与显式默认构造函数调用

为什么C ++隐式调用父默认构造函数?

默认构造函数与隐式构造函数

构造函数隐式删除

gmock调用到隐式删除的副本构造函数

隐式调用复制构造函数?

用户定义的构造函数和隐式默认构造函数

构造函数异常和隐式删除

如何隐式调用类的默认函数

通过调用隐式默认构造函数实例化给定类的对象

为什么你会 =delete 隐式删除默认构造函数,重点是什么?

显式默认的默认构造函数被隐式删除,因为 unordered_map 与结构一起用作键

C ++ 11:默认构造函数:隐式还是显式?

错误:使用auto调用unique_ptr的隐式删除副本构造函数

尝试将参数传递给方法时出现“调用隐式删除的副本构造函数”错误

为什么类中的ostringstream类型的成员导致“调用隐式删除的复制构造函数”错误?

C ++隐式和显式继承构造函数调用

隐式与显式删除副本构造函数

在构造函数中调用this()隐式调用super()是真的吗?

显式删除默认构造函数的目的

AngelScript - 避免运行隐式默认构造函数

C ++隐式默认构造函数的意义是什么?

拦截C ++隐式副本构造函数,或调用其功能

在Main()之前调用的隐式静态构造函数

隐式调用超类中的无参数构造函数

隐式构造函数调用还是什么?

如何隐式调用父类的构造函数

隐式调用函数

Java错误:未为默认构造函数定义隐式超级构造函数