| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1865 人关注过本帖
标题:这个类在程序有错误,大神帮忙指点一下。
只看楼主 加入收藏
yuyu12409
Rank: 2
等 级:论坛游民
帖 子:9
专家分:46
注 册:2017-3-12
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:2 
这个类在程序有错误,大神帮忙指点一下。
头文件程序:
#ifndef DTIME_H
#define DTIME_H
#include<iostream>
using namespace std;
class DigitalTime
{
public:
    DigitalTime(int theHour,int theMinute);
    DigitalTime();
    int getHour() const;
    int getMinute() const;
    void advance(int minutesAdded);
    void advance(int hoursAdded,int minutesAdded);
    friend bool operator==(const DigitalTime& time1,
                            const DigitalTime& time2);
    friend istream operator>>(istream& ins,DigitalTime& theObject);
    friend ostream operator<<(ostream& outs,const DigitalTime& theObject);
private:
    int hour;
    int minute;
    static void readHour(int& theHour);
    static void readMinute(int& theMinute);
    static int digitToInt(char c);
};
#endif // DTIME_H
函数程序:
#include "dtime.h"
#include<iostream>
#include<cctype>
#include<cstdlib>
using namespace std;
DigitalTime::DigitalTime(int theHour, int theMinute)
{
    if(theHour<0||theHour>24||theMinute<0||theMinute>59)
    {
        cout<<"Illegal argument to DigitalTime constructor.";
        exit(1);
    }
    else
    {
        hour=theHour;
        minute=theMinute;
    }
    if(hour==24)
    {
        hour=0;
        minute=0;
    }
}
DigitalTime::DigitalTime()
{
    hour=0;
    minute=0;
}
int DigitalTime::getHour()const
{
    return hour;
}
int DigitalTime::getMinute() const
{
    return minute;
}
void DigitalTime::advance(int minutesAdded)
{
    int grossMinutes=minute+minutesAdded;
    minute=grossMinutes%60;
    int hourAdjustment=grossMinutes/60;
    hour=(hour+hourAdjustment)%24;
}
void DigitalTime::advance(int hoursAdded, int minutesAdded)
{
    hour=(hour+hoursAdded)%24;
    advance(minutesAdded);
}
bool operator ==(const DigitalTime& time1,const DigitalTime& time2)
{
    return (time1.hour==time2.hour&&time1.minute==time2.minute);
}
istream& operator <<(istream& ins,DigitalTime& theObject)
{
    DigitalTime::readHour(theObject.hour);
    DigitalTime::readMinute(theObject.minute);
    return ins;
}
ostream& operator >>(ostream& outs,DigitalTime& theObject)
{
    outs<<theObject.hour<<":";
    if(theObject.minute<10)
        outs<<'0';
    outs<<theObject.minute;
    return outs;
}
int DigitalTime::digitToInt(char c)
{
    return (static_cast<int>(c)-static_cast<int>('0'));
}
void DigitalTime::readMinute(int &theMinute)
{
    char c1,c2;
    cin>>c1>>c2;
    if(!(isdigit(c1)&&isdigit(c2)))
    {
        cout<<"Error:illegal input to readMinute\n";
        exit(1);
    }
    theMinute=digitToInt(c1)*10+digitToInt(c2);
    if(theMinute<0||theMinute>59)
    {
        cout<<"Error:illegal input to readMinute\n";
        exit(1);
    }
}
void DigitalTime::readHour(int &theHour)
{
    char c1,c2;
    cin>>c1>>c2;
    if(!(isdigit(c1)&&(isdigit(c2)||c2==':')))
    {
        cout<<"Error:illegal input to readHour\n";
        exit(1);
    }
    if(isdigit(c1)&&c2==':')
    {
        theHour=DigitalTime::digitToInt(c1);
    }
    else
    {
        theHour=DigitalTime::digitToInt(c1)*10
                +DigitalTime::digitToInt(c2);
        cin>>c2;
        if(c2!=':')
        {
            cout<<"Error: illegal input to readHour\n";
            exit(1);
        }
    }
    if(theHour==24)
        theHour=0;
    if(theHour<0||theHour>23)
    {
        cout<<"Error: illegal input to readHour\n";
        exit(1);
    }
}
主函数程序:
#include<iostream>
using namespace std;
#include"dtime.h"
int main()
{
    DigitalTime clock,oldClock;
    cout<<"You may write midnight as either 0:00 or 24:00,\n"
       <<"but I will always write it as 0:00.\n"
      <<"Enter the time in 24-hour notation:";
    cin>>clock;
    oldClock=clock;
    clock.advance(15);
    if(clock==oldClock)
        cout<<"Something is wrong.";
    cout<<"You entered "<<oldClock<<endl;
    cout<<"15 minutes later the time will be"<<clock<<endl;
    clock.advance(2,15);
    cout<<"2 hours and 15 minutes after that\n"
       <<"the time will be "
      <<clock<<endl;
    return 0;
}


程序中istream和ostream函数部分有错误应该怎么改

搜索更多相关主题的帖子: include public friend 
2017-03-14 09:46
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9024
专家分:54030
注 册:2011-1-18
收藏
得分:20 
少贴些不相干的代码,看得眼花

friend istream operator>>(istream& ins,DigitalTime& theObject);
friend ostream operator<<(ostream& outs,const DigitalTime& theObject);
改为
friend istream& operator>>(istream& ins,DigitalTime& theObject);
friend ostream& operator<<(ostream& outs,const DigitalTime& theObject);

istream& operator <<(istream& ins,DigitalTime& theObject)
ostream& operator >>(ostream& outs,DigitalTime& theObject)
改为
istream& operator >>(istream& ins,DigitalTime& theObject)
ostream& operator <<(ostream& outs,const DigitalTime& theObject)
2017-03-14 09:58
yuyu12409
Rank: 2
等 级:论坛游民
帖 子:9
专家分:46
注 册:2017-3-12
收藏
得分:0 
回复 2楼 rjsp
多谢大神
2017-03-14 10:11
快速回复:这个类在程序有错误,大神帮忙指点一下。
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.046717 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved