C++ 使用其他2个类作为参数创建构造函数类
我正在尝试创建另一个名为CorCaixa的类,意思是ColorBox。它有两个私有属性: 我不明白为什么,当我以CaixaCorponto p1、Cor c1的形式编写构造函数时,编译器会出现以下错误: 其他日志行是关于我之前声明的3个构造函数的。 编译器似乎理解我试图创建一个ponto类型的对象,而不是说这是一个将ponto作为参数接收的构造函数。Cor类也会发生同样的情况。 在C++程序设计语言中,他在第10章PG242中使用了类似的思想: 为什么我的例子是错的而他的是对的C++ 使用其他2个类作为参数创建构造函数类,c++,class,parameters,constructor,C++,Class,Parameters,Constructor,我正在尝试创建另一个名为CorCaixa的类,意思是ColorBox。它有两个私有属性: 我不明白为什么,当我以CaixaCorponto p1、Cor c1的形式编写构造函数时,编译器会出现以下错误: 其他日志行是关于我之前声明的3个构造函数的。 编译器似乎理解我试图创建一个ponto类型的对象,而不是说这是一个将ponto作为参数接收的构造函数。Cor类也会发生同样的情况。 在C++程序设计语言中,他在第10章PG242中使用了类似的思想: 为什么我的例子是错的而他的是对的 class
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;
};
我想说的是,您没有实现默认构造函数。和/或认为应该使用指针。ponto是什么?似乎它没有默认构造函数,编译器找不到ponto::ponto。此外,Cor的默认构造函数未定义且未实现。因此,CaixaCor的构造函数失败。如果将其更改为CaixaCorponto p1,Cor c1:p0p1,c0c1{},则可能不需要默认构造函数。ponto是笛卡尔坐标系中2D点的类。这就是为什么X和Y变量。如果我定义一个ponto构造函数,这个构造函数能接收那种类型的对象吗?@WhyCry-为什么你认为指针对这个想法更有趣?你能给我举个例子吗?
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()'|
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;
};