我完全是C++的新手,所以我希望你能幫助我。 我已經用Java編寫過,並且有很多相似之處,但我無法理解這個錯誤。 看來,在我的PrimitiveBase.cpp的構造函數中,編譯器將Type Color解釋爲一個沒有參數的構造函數,我沒有這個參數。 當我定義第二個構造函數沒有參數和功能時,錯誤消失了。 但是這與我對函數的參數decleration的理解不符。
她是錯誤:
/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
這裏的代碼,你可能需要: 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 R最後不明白,因爲我在過去的2年中使用了Java,而且在上一個練習中它也使用了C++。
會很高興,如果你能幫助我:)
您需要使用* MEM-初始化列表*構造函數的參數列表後*構造一個類的任何數據成員。否則,它們將被默認構造:'PrimitiveBase :: PrimitiveBase(std :: vector coord,Color color):coord(coord),color(color){}'(即使參數名稱和數據成員是相同的;但我更願意給他們不同的名字) –
dyp
你能告訴我們對於PrimitiveBase hh嗎? –
DyP哇,謝謝。這是一個非常快速的答案,它有所幫助。 –