Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call to implicitly-deleted default constructor

Tags:

c++

stdarray

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.

like image 318
Itsbananas Avatar asked Jul 01 '15 17:07

Itsbananas


People also ask

Can a default constructor be called implicitly?

Eligible default constructorA default constructor is eligible if it is either user-declared or both implicitly-declared and definable.

Is default constructor mandatory in C++?

The compiler-defined default constructor is required to do certain initialization of class internals. It will not touch the data members or plain old data types (aggregates like an array, structures, etc…). However, the compiler generates code for the default constructor based on the situation.

What is purpose of default constructor?

A default constructor in Java is created by the compiler itself when the programmer doesn't create any constructor. The purpose of the default constructor is to initialize the attributes of the object with their default values.


1 Answers

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)
{}
like image 132
xcvr Avatar answered Sep 16 '22 15:09

xcvr