求解哪里出错了 运算符重载

Marvelous1988 2013-05-10 12:27:40
#include <iostream>
#include <cmath>
using namespace std;
class MyTime{
private:
int hours;
int mins;
public:
MyTime();
MyTime(int h,int m);
void addMins(int m);
void addHours(int h);
void display();
MyTime operator-(const MyTime& mt)const ;
MyTime operator+(const MyTime& mt)const ;
bool operator>=(const MyTime& mt) const;
bool operator<=(const MyTime& mt) const;
bool operator<(const MyTime& mt) const;
bool operator!=(const MyTime& mt) const;
};
void MyTime::addMins(int m){
mins+=m;
}
void MyTime::addHours(int h){
hours+=h;
}
void MyTime::display(){
if(mins>=0){
hours+=mins/60;
mins%=60;
}
else{
hours--;
hours-=abs(mins)/60;
mins=abs(mins)%60;
}
cout<<hours<<endl;
cout<<mins<<endl;
}
MyTime MyTime::operator+(const MyTime& mt) const{
MyTime T;
T.mins=mins+mt.mins;
T.hours=hours+mt.hours;
return T;
}
MyTime MyTime::operator-(const MyTime & mt) const{
MyTime T;
T.mins=mins-mt.mins;
T.hours=mins-mt.mins;
return T;
}
bool MyTime::operator>=(const MyTime & mt) const{
return (hours*60+mins)>=(mt.hours*60+mt.mins);
}

bool MyTime::operator<(const MyTime & mt) const{
return (hours*60+mins)<(mt.hours*60+mt.mins);
}

bool MyTime::operator<=(const MyTime & mt) const{
return (hours*60+mins)<=(mt.hours*60+mt.mins);
}

bool MyTime::operator!=(const MyTime & mt) const{
return (hours*60+mins)!=(mt.hours*60+mt.mins);
}
...全文
67 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
折翼断JJ 2013-05-10
  • 打赏
  • 举报
回复
#include <iostream>
#include <cmath>
using namespace std;
class MyTime{
    private:
        int hours;
        int mins;
    public:
        MyTime();
        MyTime(int h,int m);
        void addMins(int m);
        void addHours(int h);
        void display();
        MyTime operator-(const MyTime & mt)const ;
        MyTime operator+(const MyTime& mt)const ;
        bool operator>=(const MyTime& mt) const;
        bool operator<=(const MyTime& mt) const;
        bool operator<(const MyTime& mt) const;
        bool operator!=(const MyTime& mt) const;
        };
int main()//为了完整加的
{
	MyTime mytime(5,3);
	mytime.display();
	return 0;
}
MyTime::MyTime()//这个函数原来没定义,加上就好了
{
	hours=mins=0;
}
MyTime::MyTime(int h,int m)//这个也是
{
 hours=h;
 mins=m;
}
void MyTime::addMins(int m){
        mins+=m;
}
void MyTime::addHours(int h){
        hours+=h;
}
void MyTime::display(){
    if(mins>=0){
        hours+=mins/60;
        mins%=60;
	}
	else{
		hours--;
		hours-=abs(mins)/60;
		mins=abs(mins)%60;
	}
    cout<<hours<<endl;
    cout<<mins<<endl;
}
MyTime MyTime::operator+(const MyTime& mt) const{
	MyTime T;
	T.mins=mins+mt.mins;
	T.hours=hours+mt.hours;
	return T;
}
MyTime MyTime::operator-(const MyTime & mt) const{
	MyTime T;
	T.mins=mins-mt.mins;
	T.hours=mins-mt.mins;
	return T;
}
bool MyTime::operator>=(const MyTime & mt) const{
    return (hours*60+mins)>=(mt.hours*60+mt.mins);
}

bool MyTime::operator<(const MyTime & mt) const{
    return (hours*60+mins)<(mt.hours*60+mt.mins);
}

bool MyTime::operator<=(const MyTime & mt) const{
    return (hours*60+mins)<=(mt.hours*60+mt.mins);
}

bool MyTime::operator!=(const MyTime & mt) const{
    return (hours*60+mins)!=(mt.hours*60+mt.mins);
}
Marvelous1988 2013-05-10
  • 打赏
  • 举报
回复
这道题是帮朋友问的 我还没学到这 帮不了他 感谢大家
derekrose 2013-05-10
  • 打赏
  • 举报
回复
难道你认为构造函数是不用定义的吗?还是说你希望编译器帮你定义?
hugett 2013-05-10
  • 打赏
  • 举报
回复

#include <iostream>
#include <cmath>
using namespace std;
class MyTime{
    private:
        int hours;
        int mins;
    public:
		MyTime(){}//
		MyTime(int h,int m): hours(h), mins(m){}//这两个函数没定义
        void addMins(int m);
        void addHours(int h);
        void display();
        MyTime operator-(const MyTime& mt)const ;
        MyTime operator+(const MyTime& mt)const ;
        bool operator>=(const MyTime& mt) const;
        bool operator<=(const MyTime& mt) const;
        bool operator<(const MyTime& mt) const;
        bool operator!=(const MyTime& mt) const;
        };
void MyTime::addMins(int m){
        mins+=m;
}
void MyTime::addHours(int h){
        hours+=h;
}
void MyTime::display(){
    if(mins>=0){
        hours+=mins/60;
        mins%=60;
        }
else{
    hours--;
    hours-=abs(mins)/60;
    mins=abs(mins)%60;
}
    cout<<hours<<endl;
    cout<<mins<<endl;
}
MyTime  MyTime::operator+(const MyTime& mt) const{
        MyTime T;
        T.mins=mins+mt.mins;
        T.hours=hours+mt.hours;
        return T;
}
MyTime MyTime::operator-(const MyTime & mt) const{
        MyTime T;
        T.mins=mins-mt.mins;
        T.hours=mins-mt.mins;
        return T;
}
bool MyTime::operator>=(const MyTime & mt) const{
    return (hours*60+mins)>=(mt.hours*60+mt.mins);
}

bool MyTime::operator<(const MyTime & mt) const{
    return (hours*60+mins)<(mt.hours*60+mt.mins);
}

bool MyTime::operator<=(const MyTime & mt) const{
    return (hours*60+mins)<=(mt.hours*60+mt.mins);
}

bool MyTime::operator!=(const MyTime & mt) const{
    return (hours*60+mins)!=(mt.hours*60+mt.mins);
}

64,676

社区成员

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

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