C++习题求助大神 感激不尽 设计一个复数类 我的代码如下
classComplex{public:Complex(floatrnumber=0,intinumber=0);floatrnumber;intinumber;//vo...
class Complex{
public:
Complex(float rnumber=0,int inumber=0);
float rnumber;
int inumber;
//void add(Complex& c);
Complex add(Complex& c);
Complex(Complex &p);
void show();
private:
float real_number;
int imaginary_number;
};
Complex::Complex(Complex &p){
real_number = p.real_number;
imaginary_number = p.imaginary_number;
}
Complex::Complex(float rnumber,int inumber){
real_number = rnumber;
imaginary_number = inumber;
}
Complex Complex::add(Complex& c){
real_number += c.real_number;
imaginary_number += c.imaginary_number;
return c;
}
void Complex::show(){
if(imaginary_number == 0){
cout << real_number << endl;
}
else{
cout << real_number <<(imaginary_number>0? '+' : '-') << imaginary_number << "i" << endl;
}
}
int main(){
Complex c1(3,5);
Complex c2=4.5;
c1.add(c2);
c1.show();
return 0;
}
报错是c2 no matching function for call to ‘Complex::Complex(Complex)’ 展开
public:
Complex(float rnumber=0,int inumber=0);
float rnumber;
int inumber;
//void add(Complex& c);
Complex add(Complex& c);
Complex(Complex &p);
void show();
private:
float real_number;
int imaginary_number;
};
Complex::Complex(Complex &p){
real_number = p.real_number;
imaginary_number = p.imaginary_number;
}
Complex::Complex(float rnumber,int inumber){
real_number = rnumber;
imaginary_number = inumber;
}
Complex Complex::add(Complex& c){
real_number += c.real_number;
imaginary_number += c.imaginary_number;
return c;
}
void Complex::show(){
if(imaginary_number == 0){
cout << real_number << endl;
}
else{
cout << real_number <<(imaginary_number>0? '+' : '-') << imaginary_number << "i" << endl;
}
}
int main(){
Complex c1(3,5);
Complex c2=4.5;
c1.add(c2);
c1.show();
return 0;
}
报错是c2 no matching function for call to ‘Complex::Complex(Complex)’ 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询