error: no matching function for call to Coordinate3d::Coordinate3d()'

Started by
0 comments, last by Aardvajk 15 years, 8 months ago
Hey, thanks for reading. I have this error: 'error: no matching function for call to `Coordinate3d::Coordinate3d()' Which occurs in the first line of the constructor function in the code below:

class IntDirection3d{
    Coordinate3d c;
    bool sign;

    public:
    IntDirection3d(Coordinate3d cpar, bool signpar){
        c = cpar; sign = signpar;
    }

    Coordinate3d GetCoordinate(){
        return c;
    };

    bool GetSign(){
        return sign;
    }
};
But I don't understand why the compiler requires me to define the function 'Coordinate3d::Coordinate3d()'. I don't see why it would be needed by IntDirection3d. Below is the Coordinate3d class, in which I have, on purpose, not defined the function 'Coordinate3d::Coordinate3d()'.

enum Coordinate3dValues {X,Y,Z};
class Coordinate3d{
    Coordinate3dValues value;

    public:
    Coordinate3d (Coordinate3dValues valuepar){
        value = valuepar;
    }

    Coordinate3dValues Get(){
        return value;
    }

    Coordinate3d operator=(Coordinate3d par){
        value = par.Get();
        return *this;
    }

    Coordinate3d Swap(bool forward){
        Coordinate3dValues val = (Coordinate3dValues)((value+sign(forward))%3);
        return Coordinate3dValues(val);
    }

    Coordinate3d Next(){
        return Swap(true);
    }
    Coordinate3d Previous(){
        return Swap(false);
    }

    IntDirection3d from(bool);
};
Advertisement
Because you have defined other constructors, but not a default constructor, the compiler does not generate a default constructor for your coordinate class.

A default constructor is required when your InitDirect3d instance is created since it has to create the Coordinate3d member.

Initialiser lists are a solution here:

IntDirection3d(Coordinate3d cpar, bool signpar) : c(cpar),sign(signpar) {        /* c = cpar; sign = signpar; */    }


By doing an assignment as you were before in the constructor body, the compiler has to first construct a coordinate with a default constructor then assign to this instance with the assignment operator when it runs InitDirection3d's constructor body.

A type like a coordinate probably does want a default constructor though (that, say, sets its members to zero).

This topic is closed to new replies.

Advertisement