a,b,c都是这个类的对象
cout<<"a+c is "<< a+c <<"\n";
编译器就抱错说找不到相应的类型之类的
然后改称
d=a+c;
cout<<"a+c is "<< d <<"\n";
就好了,为啥会这样
谢谢啦
complex.cpp
#include <iostream>
#include "complex0.h"
complex::complex(double r,double i)
{
real=r;
imaginary=i;
}
complex::complex()
{
real=imaginary=0;
}
complex complex::operator+(const complex & c)const
{
return complex(real+c.real,imaginary+c.imaginary);
}
complex complex::operator-(const complex & c)const
{
return complex(real-c.real,imaginary-c.imaginary);
}
complex complex::operator*(const complex & c)const
{
return complex(real*c.real-imaginary*c.imaginary,real*c.imaginary+imaginary*c.real);
}
complex complex::operator~()const
{
return complex(real,-imaginary);
}
std::ostream & operator<<(const std::ostream & os,complex & c)
{
std::cout<<"("<<c.real<<", "<<c.imaginary<<")";
}
bool operator>>(const std::istream & is,complex & c)
{
std::cout<<"real:";
if (std::cin>>c.real)
{
std::cout<<"\nimaginary:";
std::cin>>c.imaginary;
return 1;
}
else
return 0;
}
complex operator*(double num,complex & c)
{
return complex(num*c.real,num*c.imaginary);
}
complexmain.cpp
#include <iostream>
#include "complex0.h"
int main()
{
using namespace std;
complex a(3.0,4.0);
complex c,b,d,e,f,g;
cout<<"Enter a complex number (q to quti):\n";
while(cin>>c)
{
cout<<"c is "<< c <<"\n";
b=~c;
cout<<"complex conjugate is " << b <<"\n";
cout<<"a is "<< a <<"\n";
d=a+c;
cout<<"a+c is "<< d <<"\n";
e=a-c;
cout<<"a-c is "<< e <<"\n";
f=a*c;
cout<<"a*c is "<< f <<"\n";
g=2*c;
cout<<"2*c is "<< g <<"\n";
cout<<"Enter a complex number (q to quit):\n";
}
cout<<"Done\n";
return 0;
}