1

Community,

I'm completly new to c++ so I hope you can help me. I've already written in Java and there are plenty of similarities, but I can't understand this error. It seems, that in the Constructor of my PrimitiveBase.cpp the compiler interprets the Type Colour as a constructor with no arguments, which I don't have. When I define a second Constructor with no arguments and no functionality the error is gone. But that doesn't match with my understanding of parameter decleration for functions.

Her is the error:

/home/christian/Documents/EBV/ebv-S03-G01/src/Graphics2D/PrimitiveBase.cpp: In constructor 'PrimitiveBase::PrimitiveBase(std::vector<Coordinate>, Colour)':

/home/christian/Documents/EBV/ebv-S03-G01/src/Graphics2D/PrimitiveBase.cpp:5:74: error: no matching function for call to 'Colour::Colour()'

/home/christian/Documents/EBV/ebv-S03-G01/src/Graphics2D/PrimitiveBase.cpp:5:74: note: candidates are:

/home/christian/Documents/EBV/ebv-S03-G01/src/Graphics2D/Colour.hh:9:2: note: Colour::Colour(int, int, int, std::string)

/home/christian/Documents/EBV/ebv-S03-G01/src/Graphics2D/Colour.hh:9:2: note:   candidate expects 4 arguments, 0 provided

/home/christian/Documents/EBV/ebv-S03-G01/src/Graphics2D/Colour.hh:6:7: note: Colour::Colour(const Colour&)

/home/christian/Documents/EBV/ebv-S03-G01/src/Graphics2D/Colour.hh:6:7: note:   candidate expects 1 argument, 0 provided

Here the code you might need: PrimitiveBase.cpp

#include <Graphics2D/PrimitiveBase.hh>
#include <Graphics2D/Coordinate.hh>
#include <Graphics2D/Colour.hh>

PrimitiveBase::PrimitiveBase(std::vector<Coordinate> coord, Colour colour) {    //Here the error happens
this->coord=coord;
this->colour=colour;
}

Colour PrimitiveBase::GetColour() {

return colour;

}

std::vector<Coordinate> PrimitiveBase::GetCoordinates() {

return coord;

}

void PrimitiveBase::SetColour(int red, int green, int blue) {

colour.SetColours(red,green,blue);

}

void PrimitiveBase::SetCoordinates(std::vector<Coordinate> newCoord) {

coord = newCoord;

}

Colour.hh

#ifndef COLOUR_HH_
#define COLOUR_HH_

#include <string>

 class Colour {

public:
Colour(int red, int green, int blue, std::string name);
//Colour(){};   Constructo I seem to need
//~Colour();
void SetColours(int red, int green, int blue);
static Colour black();
static Colour red();
static Colour green();
static Colour blue();

private:
std::string name;
unsigned char rgb[3];

};

#endif

I really don't understand, cause I did this for the last 2 years in Java and in the last exercises it also worked in c++.

Would be very glad if you could help me :)

4

3 回答 3

3

You have a Colour data member, and you are not initializing it in the constructor. Therefore an attempt to call its default constructor is made. There is no default constructor, so you get a compilation error. To initialize it, use the constructor initialization list:

PrimitiveBase::PrimitiveBase(std::vector<Coordinate> coord, Colour colour) 
  : coord(coord), colour(colour) // HERE
{

}

Once you are in the constructor's body, all instances have been initialized, either implicitly or explicitly. You can only modify them.

于 2013-11-15T10:35:04.480 回答
2

You need to use an initialiser list. Otherwise, before you get into the body of your constructor, the colour will have been default constructed.

PrimitiveBase::PrimitiveBase(std::vector<Coordinate> coord, Colour colour) :
coord(coord),
colour(colour)
{
}
于 2013-11-15T10:35:08.630 回答
0

I would say that you lac Colour default constructor, because you're already providing a parametrized constructor so, you need to define a default one with no paramenters or properly initialize PrimitiveBase's objects:

Something like:

Colour() 
    : name("") 
    , rgb()
{}

or:

PrimitiveBase::PrimitiveBase(std::vector<Coordinate> coord, Colour colour) 
: coord(coord)
, colour(colour)
{}

On the other hand, is always better to initialize your class' objects in your constructor's initialization list rather than with assignment in constructor's body: Member Initialization List over Assignment

于 2013-11-15T10:44:07.267 回答