我正在尝试创建另一个名为CorCaixa的类,这意味着“ColorBox”。它有2个私有属性:
1 Cor object c0("Cor" means Color); <br>
1 ponto object p0("ponto" means point)<br>
我不明白为什么,当我将构造函数编写为CaixaCor(ponto p1,Cor c1)时,编译器会出现以下错误:
C:\Users\Vinicius7\Desktop\Vinicius\CC\CG\Canvas-HSL\src\canvas drawing.cpp||In constructor 'CaixaCor::CaixaCor(ponto, Cor)':|<br>
C:\Users\Vinicius7\Desktop\Vinicius\CC\CG\Canvas-HSL\src\canvas drawing.cpp|140|error: no matching function for call to 'ponto::ponto()'|
其他日志行是关于我之前声明过的3个构造函数。
似乎编译器理解我正在尝试创建一个类型为ponto的对象,而不是实际上说这是一个将ponto作为参数接收的构造函数。 Cor类也会发生同样的事情。
在Stroustrup“C ++编程语言”中,他在第10章第42页使用了类似的想法:
class Date_and_Time{<br>
private:<br>
Date d;<br>
Time t;<br>
public:<br>
Date_and_Time(Date d, Time t);<br>
};
为什么我的例子出错了,他的说法是对的?
class Cor{
public:
Cor();
Cor(float r, float g, float b){
this->r = r;
this->g = g;
this->b = b;
}
inline float get_r(){
return this->r;
}
inline float get_g(){
return this->g;
}
inline float get_b(){
return this->b;
}
void operator=(const Cor& c1){
this->r = c1.r;
this->g = c1.g;
this->b = c1.b;
}
private:
float r;
float g;
float b;
};
std::ostream& operator<<(std::ostream& out, Cor c1){
return out <<"("<<c1.get_r()<<","<<c1.get_g()<<","<<c1.get_b()<<")";
}
class CaixaCor{
public:
CaixaCor(ponto p1, Cor c1){
this->p0 = p1;
this->c0 = c1;
}
private:
ponto p0;
Cor c0;
};