不知道为什么main函数中的2+z4一直报错,但是main函数是给定的不能改,有人知道怎么改吗,感谢
//友元函数计算2+z
#include<iostream>
using namespace std;
class Cmycomplex
{
public:
Cmycomplex(double a=0,double b=0);//缺省构造函数
void Set(double, double);
Show();
Cmycomplex operator+(int&);
Cmycomplex operator+(Cmycomplex &);//声明运算符+ 重载函数
friend Cmycomplex operator+(int &, Cmycomplex &);
private:
double real;//复数实部
double imag;//复数虚部
};
Cmycomplex::Cmycomplex(double a, double b)
{
real = a;
imag = b;
}
void Cmycomplex::Set(double a, double b)
{
real = a;
imag = b;
}
Cmycomplex Cmycomplex :: operator+(Cmycomplex &c2)
{
return Cmycomplex(real+c2.real,imag+c2.imag);
}
Cmycomplex Cmycomplex::operator+(int&i)
{
return Cmycomplex(real+i,imag);
}
Cmycomplex operator+(int &m, Cmycomplex &n)
{
return Cmycomplex(m+n.real,n.imag);
}
Cmycomplex::Show()
{
if (imag >= 0) cout << '(' << real << '+' << imag << 'i' << ')';
if (imag < 0) cout << '(' << real << "-" << imag << 'i' << ')';
}
//StudybarCommentBegin
int main()
{
Cmycomplex z1(3,4),z2(7),z3,z4(z1);
double x,y;
cin>>x>>y;
z3.Set(x,y);
cout<<endl;
z3=z3+z2;
z3.Show();
//int i=2;
z4=2+z4;
cout<<endl;
z4.Show();
z4=z4+2;
cout<<endl;
z4.Show();
}
//StudybarCommentEnd