gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 16:35:28 25 4
gpt4 key购买 nike

当我尝试编译我的 C++ 项目时,我收到错误消息Call to implicitly-deleted default constructor of '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;
}

有人能告诉我这里有什么问题吗?我找到了相似的主题,但实际上并不相同,而且我不理解给出的解释。

谢谢。

最佳答案

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

因为您没有告诉编译器如何初始化 cp,它会尝试调用 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 = 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)
{}

关于c++ - 调用隐式删除的默认构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31168135/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com