views:

171

answers:

2

I've got a compiler error but I can't figure out why.

the .hpp:

#ifndef _CGERADE_HPP
#define _CGERADE_HPP
#include "CVektor.hpp"
#include <string>

class CGerade
{

protected:
    CVektor o, rv;

public:

    CGerade(CVektor n_o, CVektor n_rv);

    CVektor getPoint(float t);

    string toString();
};

the .cpp:

#include "CGerade.hpp"

CGerade::CGerade(CVektor n_o, CVektor n_rv)
{
    o = n_o;
    rv = n_rv.getUnitVector();
}

the error message:

CGerade.cpp:10: error: no matching function for call to ‘CVektor::CVektor()’
CVektor.hpp:28: note: candidates are: CVektor::CVektor(float, float, float)
CVektor.hpp:26: note:                 CVektor::CVektor(bool, float, float, float)
CVektor.hpp:16: note:                 CVektor::CVektor(const CVektor&)
CGerade.cpp:10: error: no matching function for call to ‘CVektor::CVektor()’
CVektor.hpp:28: note: candidates are: CVektor::CVektor(float, float, float)
CVektor.hpp:26: note:                 CVektor::CVektor(bool, float, float, float)
CVektor.hpp:16: note:                 CVektor::CVektor(const CVektor&)
+2  A: 

From the looks of it, your CVektor class has no default constructor, which CGerade uses in your constructor:

CGerade::CGerade(CVektor n_o, CVektor n_rv)
{ // <-- by here, all members are constructed
    o = n_o;
    rv = n_rv.getUnitVector();
}

You could (and probably should) add one, but better is to use the initialization list to initialize members:

CGerade::CGerade(CVektor n_o, CVektor n_rv) :
o(n_o),
rv(n_rv.getUnitVector())
{}

Which specifies how the members are initialized. (And above, it was defaulting to the non-existent default-constructor.)

GMan
I don't agree with the "and probably should". It may make perfect sense for `CVektor` to have no default constructor.
Gorpik
@Gorpik: Why not? (0, 0, 0) is perfectly default.
GMan
@GMan - Save the Unicorns: In spite of the name, we don't really know what is the purpose of class `CVektor`. There are lots of classes around which should not have a default constructor.
Gorpik
@Gorpik: The function that returns a `CVektor` is called `getPoint`, `CVektor` has a member called `getUnitVector`, and it has a constructor that takes 3 floats. I think it's pretty obvious it's a normal 3-Vector class.
GMan
+2  A: 

Your CVektor class has no default constructor (that is, one that takes no arguments), so your CGerade constructor can't call it. Since you're not explicitly calling a CVektor constructor in your initializer list, the compiler implicitly tries to call the default constructor, but there is none to call, so you get an error.

You should explicitly call the CVektor copy constructor using an initializer list:

CGerade::CGerade(CVektor n_o, CVektor n_rv)
    : o(n_o), rv(n_rv.getUnitVector())
{
    // empty body
}
Adam Rosenfield