M
Mark Pfeif
Im a student in a data structures class. I am currently trying to
build a class which uses overloaded binary operators. I am also
trying to understand the use of the #ifndef directive. The following
is my code:
#ifndef Time
#define Time
#include <algorithm>
class Time
{
public:
enum am_pm
{AM = 0, PM = 12};
//class constructors
Time()
{
hours = 12;
minutes = 0;
dayTime = am_pm::AM;
}
Time(unsigned int curHours, unsigned int curMinutes, am_pm
curDayTime)
{
setHours(curHours);
setMinutes(curMinutes);
dayTime = curDayTime;
}
//set methods
void setHours(unsigned int curHours)
{
if(curHours >= 0 && curHours <= 12)
hours = curHours;
}
void setMinutes(unsigned int curMinutes)
{
if(curMinutes >= 0 && curMinutes <=59)
minutes = curMinutes;
}
//get methods
unsigned int getHours()
{return hours;}
unsigned int getMinutes()
{return minutes;}
am_pm getDayTime()
{return dayTime;}
//member functions
//overloaded operator methods
bool operator==(Time &another)
{return Time::convertToMinutes() == another.convertToMinutes();}
bool operator > (Time &another)
{return Time::convertToMinutes() > another.convertToMinutes();}
bool operator < (Time &another)
{return Time::convertToMinutes() < another.convertToMinutes();}
private:
unsigned int hours;
unsigned int hours24;
unsigned int minutes;
am_pm dayTime;
//member methods
unsigned int convertToMinutes()
{return (hours + dayTime) * 60 + minutes;}
};
#endif //define Time
when I try to compile this I get the following error:
time.h(13) : error C2059: syntax error : '{'
Line 13 is the opening bracket of my class constructor.
Any help would be greatly appreciated. If I dont use the #ifndef
directive then the class compiles. I am mystified.
Thanks, Mark
build a class which uses overloaded binary operators. I am also
trying to understand the use of the #ifndef directive. The following
is my code:
#ifndef Time
#define Time
#include <algorithm>
class Time
{
public:
enum am_pm
{AM = 0, PM = 12};
//class constructors
Time()
{
hours = 12;
minutes = 0;
dayTime = am_pm::AM;
}
Time(unsigned int curHours, unsigned int curMinutes, am_pm
curDayTime)
{
setHours(curHours);
setMinutes(curMinutes);
dayTime = curDayTime;
}
//set methods
void setHours(unsigned int curHours)
{
if(curHours >= 0 && curHours <= 12)
hours = curHours;
}
void setMinutes(unsigned int curMinutes)
{
if(curMinutes >= 0 && curMinutes <=59)
minutes = curMinutes;
}
//get methods
unsigned int getHours()
{return hours;}
unsigned int getMinutes()
{return minutes;}
am_pm getDayTime()
{return dayTime;}
//member functions
//overloaded operator methods
bool operator==(Time &another)
{return Time::convertToMinutes() == another.convertToMinutes();}
bool operator > (Time &another)
{return Time::convertToMinutes() > another.convertToMinutes();}
bool operator < (Time &another)
{return Time::convertToMinutes() < another.convertToMinutes();}
private:
unsigned int hours;
unsigned int hours24;
unsigned int minutes;
am_pm dayTime;
//member methods
unsigned int convertToMinutes()
{return (hours + dayTime) * 60 + minutes;}
};
#endif //define Time
when I try to compile this I get the following error:
time.h(13) : error C2059: syntax error : '{'
Line 13 is the opening bracket of my class constructor.
Any help would be greatly appreciated. If I dont use the #ifndef
directive then the class compiles. I am mystified.
Thanks, Mark