Header files, one can't work without the other & vice versa

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
JS Lemming
Game Developer
Game Developer
Posts: 2383
Joined: Fri May 21, 2004 4:09 pm
Location: C:\CON\CON

Header files, one can't work without the other & vice versa

Post by JS Lemming »

Welp, fell on my face. This one is kinda hard to explain.

I have stage class and I have various stage object classes (like hector and pipes). I need to pass an instance of stage to these stage objects but it requires that stage.h be included in these stage objects header files BUT stage.h needs to include these stage object headers in order to declare lists of them.

stage.h

Code: Select all

#include "gyroPipe.h"

using namespace std;

class stage
{
	public:
		list<gyroPipe*> gyroPipes;
		list<gyroPipe*>::iterator iGyroPipes;
...
gyroPipe.h

Code: Select all

#include "stage.h"

class gyroPipe
{
	public:
		gyroPipe(stage *stageInstance, int argColor, bool argVertical, int argX, int argY, int argLength);
...
I thought maybe extern could help me out here but I'm not sure how that would work.
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Header files, one can't work without the other & vice versa

Post by MarauderIIC »

Define a class prototype.

IE:

gyropipe.h

Code: Select all

#ifndef GYROPIPE_H //Standard include guards
#define GYROPIPE_H

//Only #include stage.h if you really need to
class Stage; //class prototype

class GyroPipe {
 GyroPipe(Stage* stageInstance...)
};
#endif
stage.h

Code: Select all

#ifndef STAGE_H
#define STAGE_H

//Only #include "gyropipe.h" if you really need to
class GyroPipe; //class prototype

class Stage {
 ....
};
#endif
Also, in C++, classes usually start with capital letters, as above. That way you can do Stage* stage, for instance
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Header files, one can't work without the other & vice versa

Post by Ginto8 »

From your post, can't see anything wrong, except add a few things to prevent ambiguity:

at the beginning of the header files, put:

Code: Select all

#ifndef [filename]_H
#define [filename]_H
where [filename] is the filename without ".h" (in caps). This makes sure that the content only appears once.

At the end of the files, put:

Code: Select all

#endif
Lazyfoo does a better job at explaining it, here.
Last edited by Ginto8 on Mon Jan 19, 2009 2:30 pm, edited 1 time in total.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Header files, one can't work without the other & vice versa

Post by MarauderIIC »

We must've posted at the same time :) Include guards prevent a file from being pasted into your project more than once by the compiler (causing compilation errors)
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Header files, one can't work without the other & vice versa

Post by Ginto8 »

#defines have 2 purposes: creating constants and preventing ambiguity. You have now learned one of them. :) From two people at once.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Header files, one can't work without the other & vice versa

Post by MarauderIIC »

C++ prefers const for the "creating constants," though =)
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Header files, one can't work without the other & vice versa

Post by Ginto8 »

That's true, but there's no saying you can't use #define... I wasn't saying #define was the only way to do so, or even the best. It's just that those are really the only two reasons to use them these days, seeing as macros aren't as good as inline functions
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
JS Lemming
Game Developer
Game Developer
Posts: 2383
Joined: Fri May 21, 2004 4:09 pm
Location: C:\CON\CON

Re: Header files, one can't work without the other & vice versa

Post by JS Lemming »

Thanks Mar, that's exactly what I needed.

FYI, my real code does have the guards. I just didn't post them so as to direct focus on the problem. I realize that was retarded of me.
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Header files, one can't work without the other & vice versa

Post by MarauderIIC »

Haha, np. With any luck someone that doesn't know about include guards will come across the post :)
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Header files, one can't work without the other & vice versa

Post by Ginto8 »

Well, if you have the guards, there shouldn't be any problem. You might not understand how the compiler manages to make it work, but the compiler does. ;)
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
JS Lemming
Game Developer
Game Developer
Posts: 2383
Joined: Fri May 21, 2004 4:09 pm
Location: C:\CON\CON

Re: Header files, one can't work without the other & vice versa

Post by JS Lemming »

Ginto8 wrote:Well, if you have the guards, there shouldn't be any problem. You might not understand how the compiler manages to make it work, but the compiler does. ;)
.... no. It doesn't. The way I had it didn't work. The files relied on each other in an impossible kind of way. Class prototyping fixed that.
Small girl at the harbor wrote:Look Brandon, that crab's got ham!
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Header files, one can't work without the other & vice versa

Post by Falco Girgis »

Yeah, his problem wasn't that his classes were being redefined. They weren't being defined.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: Header files, one can't work without the other & vice versa

Post by MarauderIIC »

The pattern

Class A { B b};
Class B { A a};

is what you need a class prototype to solve. This was his issue.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Header files, one can't work without the other & vice versa

Post by Ginto8 »

:oops: Okay... yeah this is one of the reasons why I'm still in the learning process. :mrgreen: I'm glad that I was wrong, actually. I learned something here. ;)
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
Post Reply