我有一个非常简单的任务要做。编写一个定义时间的类。出于某种原因,在其中一个功能中出现了一个我不理解的错误。
我搜索了一个没有成功的解决方案,所以最后我决定在这里发布。
时间.h
class time
{
private:
int _hours;
int _minutes;
float _seconds;
bool checkHours(int hours);
bool checkMinutes(int minutes);
bool checkSeconds(float seconds);
public:
time(int hours=0, int minutes=0, float seconds=0);
time(const time & tm);
~time();
void hours(int hours);
int hours() const;
void minutes(int minutes);
int minutes() const;
void seconds(float seconds);
float seconds() const;
void operator=(time tm);
bool operator==(time tm);
void print();
time getTimeFromUser();
float getTimeAsFractionOfTheDay(time tm);
};
和时间.cpp
#include <iostream>
#include "time.h"
bool time::checkHours(int hours)
{
return hours>=0 && hours<24;
}
bool time::checkMinutes(int MS)
{
return MS>=0 && MS<60;
}
bool time::checkSeconds(float MS)
{
return MS>=0 && MS<60;
}
//constractors
time::time(int hours, int minutes, float seconds)
{
if(checkHours(hours) && checkMinutes(minutes) && checkSeconds(seconds))
{
_hours=hours;
_minutes=minutes;
_seconds=seconds;
}
else
{
cout<<"Error"<<endl; _hours=-1; _minutes=-1; _seconds=-1;
}
}
time::time(const time & tm)
{
_seconds = tm.seconds();
_hours = tm.hours();
_minutes=tm.minutes();
}
time::~time()
{
}
//get-set functions
void time::hours(int hours)
{
_hours=hours;
}
int time::hours() const
{
return _hours;
}
void time::minutes(int minutes)
{
_minutes=minutes;
}
int time::minutes() const
{
return _minutes;
}
void time::seconds(float seconds)
{
_seconds = seconds;
}
float time::seconds() const
{
return _seconds;
}
//operators
void time::operator=(time tm)
{
_hours=tm.hours();
_minutes=tm.minutes();
_seconds=tm.seconds();
}
bool time::operator==(time tm)
{
return _hours=tm.hours() && _minutes==tm.minutes() && _seconds==tm.seconds();
}
//some function
void time::print()
{
cout<<" "<<_hours<<":"<<_minutes<<":"<<_seconds<<" "<<endl;
}
time time::getTimeFromUser()
{
time newTime;
int userhours=-1;
int userminutes=-1;
float userseconds=-1;
while (!checkHours(userhours))
{
cout<<"enter hours"<<endl;
cin>>userhours;
if(!checkHours(userhours))
{
cout<<"Error try again"<<endl;
}
}
while (!checkMinutes(userminutes))
{
cout<<"enter minutes"<<endl;
cin>>userminutes;
if(!checkMinutes(userminutes))
{
cout<<"Error try again"<<endl;
}
}
while (!checkSeconds(userseconds))
{
cout<<"enter Seconds"<<endl;
cin>>userseconds;
if(!checkSeconds(userseconds))
{
cout<<"Error try again"<<endl;
}
}
newTime.seconds(userseconds);
newTime.hours(userhours);
newTime.minutes(userminutes);
return newTime;
}
float time::getTimeAsFractionOfTheDay(time tm)
{
return 0.0;
}
我得到了那些错误
我不明白我做错了什么。我认为这是愚蠢的,但我找不到它。