【求教】重载运算符+出错。
//FloatNM.cpp
#include "FloatNM.h"
void main()
{
Float f1(2.4),f2(3.5);
cout<<"f1: "<<f1<<endl;
cout<<"f2: "<<f2<<endl;
cout<<"f1+f2: "<<f1+f2<<endl;
cout<<"f1*f2: "<<f1*f2<<endl;
cout<<"f1^3.2: "<<(f1^3.2)<<endl;
cout<<"f1+=f2: "<<(f1+=f2)<<endl;
}
//FloatNM.h
#ifndef FLOATNM_H
#define FLOATNM_H
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
class Float
{
friend Float operator+(const Float &Left,const Float &Right);
friend Float operator*(const Float& Left,const Float& Right);
friend Float operator^(const Float& Left,const Float& Right);
friend Float& operator+=(Float& Left,const Float& Right);
private:
float F;
public:
Float(float x):F(x){}
Float():F(0){}
operator float(){return F;}
// Float operator+(const Float& Right)
// {
// return Float(F+(Right.F));
// }
};
Float operator+(const Float& Left,const Float& Right)
{
return (Float)((Left.F)+(Right.F));
}
Float operator*(const Float& Left,const Float& Right)
{
return Float(Left.F*Right.F);
}
Float operator^(const Float& Left,const Float& Right)
{
return Float(pow(Left.F,Right.F));
}
Float& operator+=(Float& Left,const Float& Right)
{
Left.F+=Right.F;
return Left;
}
#endif
编译的时候,提示内部错误编译。我如果转换成成员函数的形式,则不会有问题,程序运行正常。甚至我把关于+的重载声明定义部分都注释了都可以正常运行。我现在想知道为什么用友函数就出问题了呢。高手帮帮忙呀。