all kinds of problems with #ifndef

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
 
B

Ben Bacarisse

Mark Pfeif said:
Im a student in a data structures class. I am currently trying to
build a class which uses overloaded binary operators.
#include <algorithm>
class Time

Looks like you want to post in comp.lang.c++. This is a C group.

<snip>
 
K

Keith Thompson

Mark Pfeif said:
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:

You wanted comp.lang.c++, not comp.lang.c, but your problem is one
that could occur in C as well.
#ifndef Time
#define Time

Here you define Time as a macro; it expands to nothing.
#include <algorithm>
class Time

What does the above line look like after macro substitution?
{
public:

enum am_pm
{AM = 0, PM = 12};
//class constructors
Time()

What does the above line look like after macro substitution?

[snip]

Macro names are conventionally all-caps; following that convention would
have avoided this problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,436
Messages
2,571,696
Members
48,796
Latest member
Greg L.
Top