all kinds of problems with #ifndef

  • Follow


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
0
Reply Mark 7/24/2010 6:02:36 PM

Mark Pfeif <mayeyeserveu@gmail.com> writes:

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

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

<snip>
-- 
Ben.
0
Reply Ben 7/24/2010 6:24:18 PM


On Sat, 24 Jul 2010 11:02:36 -0700 (PDT), Mark Pfeif
<mayeyeserveu@gmail.com> wrote:

Don't do this:

>#ifndef Time
>#define Time

Don't define Time and then expect to be able to redefine it as a
class. Drop the #define and it will compile.

In future, ask c++ in the comp.lang.c++ group next door.
0
Reply Geoff 7/24/2010 7:09:00 PM

Mark Pfeif <mayeyeserveu@gmail.com> writes:
> 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.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
Nokia
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
0
Reply kst-u (21549) 7/24/2010 8:19:18 PM

3 Replies
159 Views

(page loaded in 1.482 seconds)

Similiar Articles:













7/15/2012 2:38:18 AM


Reply: