当我尝试编译我的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;
}
有人可以告诉我这是什么问题吗?我发现了类似的主题,但并非完全相同,而且我不理解给出的解释。
谢谢。
两件事情。类成员在构造函数的主体之前初始化,并且默认构造函数是不带参数的构造函数。
因为您没有告诉编译器如何初始化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 = 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)
{}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句