调用隐式删除的默认构造函数
Call to implicitly-deleted default constructor
当我试图编译C++项目时,我得到错误消息调用隐式删除的'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)
{}
相关文章:
- 为什么在没有显式默认构造函数的情况下,将另一个结构封装在联合中作为成员的结构不能编译
- 初始化具有非默认构造函数的std::数组项的更好方法
- 具有默认模板类型的默认构造函数的类型推导
- 如何使用非默认构造函数实例化模板化类
- 有没有一种代码密度较低的方法来使用非默认构造函数初始化数组?
- 声明没有默认构造函数的字段
- 没有默认构造函数作为模板参数的自定义比较器
- C++17 没有默认构造函数的地图放置(私有默认构造函数)
- 使用移动调用对等构造函数unique_ptr默认构造函数
- C++复制构造函数和默认构造函数
- 将向量从 N1 缩小到 N2 项,而不触发默认构造函数并仅使用 move 语义
- 为什么即使我调用参数化构造函数也会调用默认构造函数?
- 具有非默认构造函数的单例类
- 在 C++ 中声明 const 对象需要用户定义的默认构造函数.如果我有一个可变成员变量,为什么不呢?
- 如何处理没有默认构造函数但在另一个构造函数中构造的对象?
- 在C++中使用默认构造函数初始化对象的不同方法
- 在没有默认构造函数的情况下创建的派生对象
- 强制使用默认构造函数对成员进行未初始化的声明
- 使用默认构造函数初始化对象的不同方法
- 创建类类型的动态分配数组,其中类不得具有默认构造函数