I get the error message Call to implicitly-deleted default constructor of 'std::array' when I try to compile my C++ project.
Header file 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
#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.
Two things. Class members are initialized before the 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
.
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 = z
doesn't make sense. You're assigning a variable to itself. this->x = x
is 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
Point3D::Point3D(float x, float y, float z)
: x(x)
, y(y)
, z(z)
{}