请教关于C++中重载的问题3

来源:百度知道 编辑:UC知道 时间:2024/06/15 11:42:43
要求重载运算符"+"使之实现复数运算 可以实现两个complex类对象相加,complex类对象与一个整形数相加,一个整形数与一个complex类对象相加均为合法的运算

编译系统VC++6.0
无法通过编译
请帮忙看下问题所在

#include <iostream>
using namespace std;
class complex
{
public:
complex(){real=0;img=0;}
complex(int a,int b):real(a),img(b){}
friend complex operator +(complex&,complex&);
friend comolex operator +(int i,complex&);
friend complex operator +(complex&,int i);
void display();
private:
int real;
int img;
};

complex operator +(complex&c1,complex& c2)
{
complex c3;
c3.real=c1.real+c2.real;
c3.img=c1.img+c2.img;
return c3;
}

complex operator +(int i,complex& c1)
{
complex c2;
c2.real=i+c1.real;
c2.img=c1.img;
return c2;
}

complex operator +(complex& c1,int i)
{
complex c2;
c2.real=i+c1.real;
c2.img=c1.img;
return c2;
}

void

vc++6.0编译器申明友元时, 需要前向申明:
class complex;
complex operator +(complex&,complex&);
comolex operator +(int i,complex&);
complex operator +(complex&,int i);
加在类定义的前面就可以了。

你看看第二个友元函数的声明,返回类型是啥?
comolex... 汗。
只此一个拼写错误而已。

我改完了就通过了。