简体   繁体   中英

C++ Interprets parameter type as Constructor

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 :)

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.

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)
{
}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM