【求教】重载运算符+出错。

wenguangzheng26 2009-11-22 04:34:52
//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
编译的时候,提示内部错误编译。我如果转换成成员函数的形式,则不会有问题,程序运行正常。甚至我把关于+的重载声明定义部分都注释了都可以正常运行。我现在想知道为什么用友函数就出问题了呢。高手帮帮忙呀。
...全文
121 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangjie199011087 2009-11-24
  • 打赏
  • 举报
回复
好像没有错!
wenguangzheng26 2009-11-24
  • 打赏
  • 举报
回复
这个问题就没有人解决过。查了很多的资料,我想应该是编译器出问题吧。在VC中要解决这个问题,只能定义成成员函数形式了。或者就是用其他的编译器。
陌上花花 2009-11-23
  • 打赏
  • 举报
回复
新手学习下
wenguangzheng26 2009-11-23
  • 打赏
  • 举报
回复
什么叫前项引用声明?解释一下吧。还有,我如果把那个加的重载注释了,或者改成成员函数的形式,其他的还是用友元函数的话没什么问题呀。
stardust20 2009-11-22
  • 打赏
  • 举报
回复
用VS2008调了下好像没错啊。。。如果LZ是用VC6.0的话,用友元的时候要前项引用声明

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧