I got a pretty simple task to do. Write a class that defines time. For some reason, in one of the functions I get an error, that I do not understand.
I searched for a solution with no success, so in the end I decided post it here.
time.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);
};
and time.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;
}
And i got those errors
I don't understand what I did wrong. I think it's something stupid but I can't find it.
return _hours=tm.hours() &&
– nneonneotime
is not a type - thus causing trouble intime time::getTimeFromUser()
. Addclass time;
before entire declaration ofclass time {};
– Vinayak Garg