#include<iostream>
using namespace std;
class complex{
private:
double real;
double imag;
public:
complex();
complex(double x,double y);
void operator~()const;
void operator+(complex & num)const;
void operator-(complex & num)const;
void operator*(complex & num)const;
friend ostream & operator<<(ostream & os,complex & num)const;
friend istream & operator>>(istream & is,complex & num);
friend void operator*(double n,complex & num);
};
int main()
{
complex a(3.0,4.0);
complex c;
cout<<"Enter a cimplex number (q tu quit):\n";
while(cin>>c)
{
cout<<"c is "<<c<<'\n';
cout<<"complex conjugate is "<<~c<<'\n';
cout<<"a is "<<a<<'\n';
cout<<"a + c is "<<a+c<<'\n';
cout<<"a - c is "<<a-c<<'\n';
cout<<"a * c is "<<a*c<<'\n';
cout<<"2 * c is "<<2*c<<'\n';
cout<<"Enter a cimplex number (q tu quit):\n";
}
cout<<"Done\n";
return 0;
}
complex::complex()
{
real=0;
imag=0;
}
complex::complex(double x,double y)
{
real=x;
imag=y;
}
void complex::operator~()const
{
complex b(real,-imag);
cout<<b;
}
void complex::operator+(complex & num)const
{
cout<<complex(real+num.real,imag+num.imag);
}
void complex::operator-(complex & num)const
{
cout<<complex(real-num.real,imag-num.imag);
}
void complex::operator*(complex & num)const
{
cout<<complex(real*num.real-imag*num.imag,real*num.imag+imag*num.real);
}
ostream & operator<<(ostream & os,complex & num)const
{
os<<'('<<num.real<<','<<num.imag<<"i)";
return os;
}
istream & operator>>(istream & is,complex & num)
{
cout<<"real:";
is>>num.real;
cout<<"imag:";
is>>num.imag;
return is;
}
void operator*(double n,complex & num)
{
cout<<complex(n*num.real,n*num.imag);
} 基本上所有的错误都指向重载的<<符号
我这是哪里错了?
using namespace std;
class complex{
private:
double real;
double imag;
public:
complex();
complex(double x,double y);
void operator~()const;
void operator+(complex & num)const;
void operator-(complex & num)const;
void operator*(complex & num)const;
friend ostream & operator<<(ostream & os,complex & num)const;
friend istream & operator>>(istream & is,complex & num);
friend void operator*(double n,complex & num);
};
int main()
{
complex a(3.0,4.0);
complex c;
cout<<"Enter a cimplex number (q tu quit):\n";
while(cin>>c)
{
cout<<"c is "<<c<<'\n';
cout<<"complex conjugate is "<<~c<<'\n';
cout<<"a is "<<a<<'\n';
cout<<"a + c is "<<a+c<<'\n';
cout<<"a - c is "<<a-c<<'\n';
cout<<"a * c is "<<a*c<<'\n';
cout<<"2 * c is "<<2*c<<'\n';
cout<<"Enter a cimplex number (q tu quit):\n";
}
cout<<"Done\n";
return 0;
}
complex::complex()
{
real=0;
imag=0;
}
complex::complex(double x,double y)
{
real=x;
imag=y;
}
void complex::operator~()const
{
complex b(real,-imag);
cout<<b;
}
void complex::operator+(complex & num)const
{
cout<<complex(real+num.real,imag+num.imag);
}
void complex::operator-(complex & num)const
{
cout<<complex(real-num.real,imag-num.imag);
}
void complex::operator*(complex & num)const
{
cout<<complex(real*num.real-imag*num.imag,real*num.imag+imag*num.real);
}
ostream & operator<<(ostream & os,complex & num)const
{
os<<'('<<num.real<<','<<num.imag<<"i)";
return os;
}
istream & operator>>(istream & is,complex & num)
{
cout<<"real:";
is>>num.real;
cout<<"imag:";
is>>num.imag;
return is;
}
void operator*(double n,complex & num)
{
cout<<complex(n*num.real,n*num.imag);
} 基本上所有的错误都指向重载的<<符号
我这是哪里错了?