CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  C/C++ >  新手乐园

高手救命啊!!!->(重载运算符出错)

楼主wuyinggu(寂寞小阳春)2006-05-01 23:58:42 在 C/C++ / 新手乐园 提问

头文件:  
  #include<iostream>  
  #include<iomanip>  
  #include<string>  
   
  using   namespace   std;  
   
   
  class   newtime24  
  {  
    public:  
  newtime24(int   h=0,   int   m   =0):hour(0),minute(0)  
  {  
  normalizeTime();  
  }  
  //一组+运算符   ++++++++++++++  
  friend   newtime24   operator+(const   newtime24&   lhs,   const   newtime24&   rhs)  
  {  
  return   newtime24(lhs.hour+rhs.hour,lhs.minute+rhs.minute);  
  }  
  friend   newtime24   operator+(const   newtime24&   lhs,int   min)  
  {  
   
  return   newtime24(lhs.hour,lhs.minute+min);  
  }  
   
  friend   newtime24   operator+(int   min,   const   newtime24&   rhs)  
  {  
  return   newtime24(rhs.hour,min+rhs.minute);  
  }  
  //   -   -   -   --   -    
  friend   newtime24   operator-   (const   newtime24&   lhs,   const   newtime24&   rhs)  
  {  
    int   currTime   =   lhs.hour*60   +lhs.minute;  
    int   tTime   =   rhs.hour*60   +   rhs.minute;  
   
          if(tTime   <   currTime)  
        throw   string("time24   duration():argument   is   an   earlier   time");  
    else  
      return   newtime24(0,tTime-currTime);  
  }  
   
  friend   bool   operator==(const   newtime24&   lhs,   const   newtime24&   rhs)  
  {  
                  return   (lhs.hour*60   +lhs.minute)==   (   rhs.hour*60   +   rhs.minute);  
  }  
  //  
  friend   bool   operator<(const   newtime24&   lhs,   const   newtime24&   rhs)  
  {  
       return   (lhs.hour*60   +lhs.minute)<(   rhs.hour*60   +   rhs.minute);  
  }  
  //   iostream  
  friend   ostream&   operator<<   (ostream&   ostr,   const   newtime24&   t)  
  {  
  ostr   <<   setfill('0')   <<   t.hour   <<   ":"   <<   setw(2)   <<   t.minute;  
  return   ostr;  
  }  
  friend   istream&   operator>>(istream&   istr,     newtime24&   t)  
  {  
  char   setparatChar;  
  istr   >>   t.hour   >>setparatChar   >>   t.minute   ;  
  t.normalizeTime();  
  return   istr;  
  }  
  //   成员函数+=  
  newtime24&   operator   +=(const   newtime24&   rhs)  
  {  
  *this   =   *this   +rhs;  
  return   *this;  
  }  
  newtime24&   operator   +=(int   min)  
  {  
  *this   =   *this   +   min;  
  return   *this;  
  }  
   
      int   getHour()   const   {return   hour;}  
   
            int   getMinute()   const   {return   minute;}  
   
   
  private:  
   
        int   hour,minute;  
        void   normalizeTime()  
        {  
        int   extraHours   =   minute   /   60;  
                minute   %=   60;  
   
        hour   =   (hour   +   extraHours   )   %   24   ;  
        }  
  };  
   
  //测试的main  
  #include   <iostream>  
  #include   "newtime24.h"  
   
  using   namespace   std;  
   
   
  int   main()  
  {  
  newtime24   apptTime,   totalApptTime;  
  int   apptLength;  
   
  int   i;  
   
  for(i=1;i<=3;i++)  
  {  
  cout   <<   "Enter   start   of   appointment   and   length   in   minutes:   "   ;  
  cin   >>   apptTime   >>   apptLength;  
   
  cout   <<   "Appointment   "   <<   i   <<":   start:   "   <<   apptTime  
            <<   "   stop:   "   <<   (apptTime   +   apptLength)   <<   endl;  
  totalApptTime   +=   apptLength;  
  }  
  cout   <<   "Total   appointment   time:   "   <<   totalApptTime   <<   endl;  
  return   0;  
  }  
  编译错误信息:  
  Compiling...  
  appt.Cpp  
  f:\simplycpp\数据结构\newtime24.h(32)   :   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.  
   
  appt.obj   -   1   error(s),   0   warning(s)  
   
  好去掉头文件的-号重载,编译成功,但是+ += 2个重载运算符不起作用!  
  apptTime   +   apptLength =0??//错误  
  totalApptTime   +=   apptLength ==0?? //错误!  
  但是输出输入>>  <<又行!!  
  请问什么原因啊??i 问题点数:20、回复次数:4Top

1 楼A_B_C_ABC(黄瓜@YouCanDoIt)回复于 2006-05-02 02:50:31 得分 20

//以下VC6中编译通过    
   
  #include<iostream>  
  #include<iomanip>  
  #include<string>  
   
  //using   namespace   std;  
  using     std::ostream;  
  using     std::istream;  
  using     std::setfill;  
  using     std::setw;  
   
  class   newtime24  
  {  
  public:  
  newtime24(int   h=0,   int   m   =0):hour(0),minute(0)  
  {  
  normalizeTime();  
  }  
  //一组+运算符   ++++++++++++++  
  friend   newtime24   operator+(const   newtime24&   lhs,   const   newtime24&   rhs);  
  friend   newtime24   operator+(const   newtime24&   lhs,int   min);  
   
  friend   newtime24   operator+(int   min,   const   newtime24&   rhs);  
  //   -   -   -   --   -    
  friend   newtime24   operator-   (const   newtime24&   lhs,   const   newtime24&   rhs);  
   
  friend   bool   operator==(const   newtime24&   lhs,   const   newtime24&   rhs);  
  //  
  friend   bool   operator<(const   newtime24&   lhs,   const   newtime24&   rhs);  
  //   iostream  
  friend   ostream&   operator<<   (std::ostream&   ostr,   const   newtime24&   t);  
  friend   istream&   operator>>(istream&   istr,     newtime24&   t);  
  //   成员函数+=  
  newtime24&   operator   +=(const   newtime24&   rhs)  
  {  
  *this   =   *this   +rhs;  
  return   *this;  
  }  
  newtime24&   operator   +=(int   min)  
  {  
  *this   =   *this   +   min;  
  return   *this;  
  }  
  int   getHour()   const   {return   hour;}  
          int   getMinute()   const   {return   minute;}  
   
  private:  
  int   hour,  
  minute;  
  void   normalizeTime()  
  {  
  int   extraHours   =   minute   /   60;  
  minute   %=   60;  
  hour   =   (hour   +   extraHours   )   %   24   ;  
  }  
  };  
  ////////////////////////////////////////////////////////  
    newtime24   operator+(const   newtime24&   lhs,   const   newtime24&   rhs)  
  {  
  return   newtime24(lhs.hour+rhs.hour,lhs.minute+rhs.minute);  
  }  
    newtime24   operator+(const   newtime24&   lhs,int   min)  
  {  
   
  return   newtime24(lhs.hour,lhs.minute+min);  
  }  
   
    newtime24   operator+(int   min,   const   newtime24&   rhs)  
  {  
  return   newtime24(rhs.hour,min+rhs.minute);  
  }  
  //   -   -   -   --   -    
    newtime24   operator-(const   newtime24&   lhs,   const   newtime24&   rhs)  
  {  
  int   currTime   =   lhs.hour*60   +lhs.minute;  
  int   tTime   =   rhs.hour*60   +   rhs.minute;  
   
  if(tTime   <   currTime)  
  throw   std::string("time24   duration():argument   is   an   earlier   time");  
  else  
  return   newtime24(0,tTime-currTime);  
  }  
   
    bool   operator==(const   newtime24&   lhs,   const   newtime24&   rhs)  
  {  
  return   (lhs.hour*60   +lhs.minute)==   (   rhs.hour*60   +   rhs.minute);  
  }  
  //  
    bool   operator<(const   newtime24&   lhs,   const   newtime24&   rhs)  
  {  
  return   (lhs.hour*60   +lhs.minute)<(   rhs.hour*60   +   rhs.minute);  
  }  
  //   iostream  
    ostream&   operator<<   (ostream&   ostr,   const   newtime24&   t)  
  {  
  ostr   <<   setfill('0')   <<   t.hour   <<   ":"   <<   setw(2)   <<   t.minute;  
  return   ostr;  
  }  
    istream&   operator>>(istream&   istr,     newtime24&   t)  
  {  
  char   setparatChar;  
  istr   >>   t.hour   >>setparatChar   >>   t.minute   ;  
  t.normalizeTime();  
  return   istr;  
  }  
  //////////////////////////////////////////////////////  
   
  //测试的main  
  #include   <iostream>  
  //#include   "newtime24.h"  
   
  //using   namespace   std;  
    using   std::cout;  
    using   std::endl;  
    using   std::cin;  
   
  int   main()  
  {  
  newtime24   apptTime,   totalApptTime;  
  int   apptLength;  
   
  int   i;  
   
  for(i=1;i<=3;i++)  
  {  
  cout   <<   "Enter   start   of   appointment   and   length   in   minutes:   "   ;  
  cin   >>   apptTime   >>   apptLength;  
   
  cout   <<   "Appointment   "   <<   i   <<":   start:   "   <<   apptTime  
  <<   "   stop:   "   <<   (apptTime   +   apptLength)   <<   endl;  
  totalApptTime   +=   apptLength;  
  }  
  cout   <<   "Total   appointment   time:   "   <<   totalApptTime   <<   endl;  
  return   0;  
  }Top

2 楼wuyinggu(寂寞小阳春)回复于 2006-05-02 20:01:21 得分 0

我试过了是行,但是结果不对,+号没有+上约会的长度,结束时间是0!!Top

3 楼wuyinggu(寂寞小阳春)回复于 2006-05-02 20:01:55 得分 0

你那样和我不是一样吗?只是放到外面来定义,2者有区别吗?Top

4 楼A_B_C_ABC(黄瓜@YouCanDoIt)回复于 2006-05-02 22:09:08 得分 0

我试过了是行,但是结果不对,+号没有+上约会的长度,结束时间是0!!  
  ===================================  
  结果不对,那是因为你的构造函数  
  newtime24(int   h=0,   int   m   =0):hour(0),minute(0)        
  {  
  normalizeTime();  
  }  
  应为  
  newtime24(int   h=0,   int   m   =0):hour(h),minute(m)  
  {  
  normalizeTime();  
  }  
  其他地方的逻辑是否正确,还没仔细看。  
   
   
  只是放到外面来定义,2者有区别吗?  
  ===================  
  这些函数本是全局函数,因为要访问类的私有成员,才声明为类的友元,放在类中定义,可能会和成员函数产生混淆。  
  Top

相关问题

关键词

得分解答快速导航

  • 帖主:wuyinggu
  • A_B_C_ABC

相关链接

  • C/C++ Blog
  • C/C++类图书
  • C/C++类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo