友元重载
#include<iostream>using namespace std;
class complex
{
public:
complex(double r = 0.0, double i = 0.0)
{
real = r;
image = i;
}
friend complex& operator+(complex c1, complex c2);
friend complex& operator-(complex c1, complex c2);
void display();
private:
double real;
double image;
};
complex& operator+(complex c1, complex c2)
{
return comlpex(c1.real + c2.real, c1.image + c2.image);
}
complex& operator-(complex c1, complex c2)
{
return comlpex(c1.real - c2.real, c1.image - c2.image);
}
void complex::display()
{
cout<<"("<<real<<","<<image<<")\n";
}
int main()
{
complex c1(5,4), c2(2,10), c3;
cout<<"c1=";
c1.display();
cout<<"c2=";
c2.display();
c3 = c1 + c2;
cout<<"c3=";
c3.display();
c3 = c1 - c2;
cout<<"c3=";
c3.display();
return 0;
}
错误提示:Compiling...
Complex1.cpp
D:\vc++\MSDev98\MyProjects\Complex1\Complex1.cpp(13) : fatal error C1001: INTERNAL COMPILER ERROR
(compiler file 'msc1.cpp', line 1786)
Please choose the Technical Support command on the Visual C++
Help menu, or open the Technical Support help file for more information
Error executing cl.exe.
Complex1.obj - 1 error(s), 0 warning(s)