#include <iostream>
using namespace std;
class Complex
{
public:
Complex()
{
real=0;
imag=0;
}
Complex(double r,double i)
{
real=r;
imag=i;
}
friend Complex operator+(Complex &c1,Complex &c2);//声明复数相加的函数
//operator+ 可以看作函数名形参为类的对象
void display();
private:
double real;
double imag;
};
Complex Complex::operator+(Complex &c1,Complex &c2)
{
return (Complex(c1.real+c2.real,c1.imag+c2.imag));
//重载函数的返回类型是Complex类型
//返回值为Complex(c1.real+c2.real,c1.imag+c2.imag)
}
void Complex::display()
{
cout <<"("<<real<<","<<imag<<"i)"<<endl;
}
int main()
{
Complex c1(3,4),c2(5,-10),c3;
c3=c1+c2;
//编译系统可以把他解释c1.operator+(c2)
//"operator+“为函数名而对象c1调用该对象
cout<<"c1=";c1.display();
cout<<"c2=";c2.display();
cout<<"c1+c2=";
c3.display();
return 0;
}
错误提示如下:
C:\Documents and Settings\ycc\桌面\compiler\itemp\fgf.cpp(18) : fatal error C1001: INTERNAL COMPILER ERROR
希望得到你的帮助谢谢谢!!!