关于友元函数的两道题

来源:百度知道 编辑:UC知道 时间:2024/07/04 11:12:09
#include <iostream>
using namespace std;

class Complex
{
public:
Complex(int a=0,int b=0):
real(a),imag(b){}

friend Complex operator + (Complex &y,Complex &x);
void output();
private:
int real;
int imag;
};

int main()
{
Complex com1(2,4),com2(-3,2),com3;

com3=com1+com2;

com1.output();
cout << '+';
com2.output();
cout <<'=';
com3.output();
cout << endl;

return 0;
}

Complex operator + (Complex &y,Complex &x)
{
return Complex(y.real+x.real,y.imag+x.imag);
}

void Complex::output()
{
cout << '(' << real << '+' << imag << 'i' << ')';
}
出现错误:
:\job\operator2\operator.cpp(10) : fatal error C1001: INTERNAL COMPILER ERROR
(c

呵呵,这个问题我是知道的,楼主使用的应该是VC6.0吧
这个版本太早了,是不支持友元函数,还有友元类的哦
楼主的代码里面使用了友元哦
推荐使用VS.NET2003以后的版本
或者DEVC++,这2个都是支持最新的哦
VC6.0出来的时候,C++好像标准都还没有定义

程序没有任何问题,应该是Visual C++编译器的错误,推荐打补丁或换用别的编译器。