Skip to main content
gamedev.net
🔒 Locked

c++ headers

Started by alexmoura Oct 28, 2000 at 7:33 PM 4 replies 250+ views
Original Post
alexmoura
alexmoura
I have these two classes whose headers look like these: Class Planet: #include "bitmap_file.h" #include "image.h" #include "resources.h" #include "ship.h" ... class planet { ... private: ... ship *in_orbit[MAX_SHIPS]; }; // end class Class Ship: class ship { ... }; // end class I want to add a planet variable to class ship, so, as you see, class planet has to be aware that class ship exists and vice-versa. My problem is, it gives me 18 compilation errors when I had the mentioned variable; if I try to include "planet.h" in the ship file, it gives even more errors. Can anyone tell me what''s going on and how can I fix this. I know it''s relationed to the headers. Thanks.
Scooter
Scooter
Make sure that all your variables and classes are public, if they''re private that limits the scope.
We are here just like this,It really doesn't matter why.The world is here like it is,'We could laugh we could cry.Is it good or is it bad,Wright or maybe wrong?There's no use of being sad,Without a purpose within a song.
Outworlder
Outworlder
Don''t know if it''s a good idea to make everything public. Maybe you''d want to consider using "friend" classes

Gaiomard Dragon
-===(UDIC)===-
Gaiomard Dragon-===(UDIC)===-
SiCrane
SiCrane
You need to create forward declarations for your classes. If class planet references class ship, then have the following:

class ship;
class planet {
// blah
ship * s;
// blah
};

Then planet can reference a pointer to a ship object without needing the full class description of ship first.
alexmoura
alexmoura
Scooter: I don''t think it''s a good policy making all variables public. For me, it just breaks the structure of the code.

Outworlder: I don''t know what are friend classes, but thank you anyway.

SiCrane: That''s exactly what I needed. It now compiles fine.

Thank you all for helping.
NextS
NextS
In my opinion you should always use forward class declarations if possible at least in the header file to reduce compilation time. This is extremely helpful in big projects. Because you limit the dependencies of the header files

Sven

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.