code wont compile because header inclusion configuration is complicated

Started by
3 comments, last by fireking 21 years, 7 months ago
I have the following files in my project ffatom.h ffbitmap.h ffcamera.h ffclass.h ffengine.h ffmap.h ffatom.cpp ffbitmap.cpp ffcamera.cpp ffengine.cpp ffmap.cpp main.cpp Some classes (defined in respective header files) need other classes, so i figure i just include the header to that class. But it doesnt work Can someone explain to me a way to make it so all i have to do is include ffclass.h in order to get EVERY class, function, variable, and whatever else included? How can i do it so that it will only include it once, if lets say ffatom.h happens to include a file that includes a file already included in ffatom.h. This is extremely complicated, and ive tried for 2 hours, different inclusion schemes, and it wont work. --Fireking Owner/Leader Genetics 3rd Dimension Development
--FirekingOwner/LeaderFiregames Development &Blackdragon Studios
Advertisement
you make already done this, but have you used #ifndef ?

The #ifdef and #ifndef directives perform the same task as the #if directive when it is used with defined( identifier ).

Syntax

#ifdef identifier

#ifndef identifier

is equivalent to

#if defined identifier

#if !defined identifier

You can use the #ifdef and #ifndef directives anywhere #if can be used. The #ifdef identifier statement is equivalent to #if 1 when identifier has been defined, and it is equivalent to #if 0 when identifier has not been defined or has been undefined with the #undef directive. These directives check only for the presence or absence of identifiers defined with #define, not for identifiers declared in the C or C++ source code.

These directives are provided only for compatibility with previous versions of the language. The defined( identifier ) constant expression used with the #if directive is preferred.

The #ifndef directive checks for the opposite of the condition checked by #ifdef. If the identifier has not been defined (or its definition has been removed with #undef), the condition is true (nonzero). Otherwise, the condition is false (0).

*** If you already knew about this sorry..just trying to help, if not enjoy...

~Bolt
~Bolt"All men dream: but not equally. Those who dream by night in the dusty recesses of their minds wake in the day to find that it was vanity: but the dreamers of the day are dangerous men, for they may act their dreams with open eyes, to make it possible." This I did...
if you have a header (which i know you do) then there are a number of things which it is good practice to do.

1) put
#ifndef HEADERFILENAME_H
#define HEADERFILENAME_H

at the beginning of the file (replace the filename with your own version... doesn''t HAVE to be like that exactly but that''s the way i do it)

also don''t forget to put
#endif

at the end of the file. If you forget this it can be a real mind bender tracking it down as a ''bug''.

2) If you have class defninitons in you header like this

class Dog : public Animal
{
void Speak(AudioStream&);
};

then the Dog will need to know about it''s base Animal class so put the Animal header file in at the beginning with

#include "Animal.h"

also the Dog class needs to know about AudioStream so the dog can speak. However as it ownly needs a reference to it (I could have used a pointer instead) then it doesn''t need to know about the internals of the AudioStream and you can simply just tell the compiler it exists with a forward declaration

class AudioStream;

So your final header will look like this


  #ifndef DOG_H#define DOG_H#include "Animal.h"class AudioStream;class Dog: public Animal{    void Speak(AudioStream&);};#endif  


3) part of programming is the discipline of understanding what you need where. putting all headers in one file means you don''t really get a feel for what depends on what. yes its easier. but there are other issues. if you make one change to any header then your whole project needs recompiling rather than just the affected code. maybe this doesn''t concern you with a small project but i find it''s a good discipline to keep up.


4) when you come to implement the Dog class in a cpp file you will first include the Dog.h header file. But you''ll find that if you use the AudioStream so that the Dog can speak you''ll have to include the relevant header. Otherwise the compiler will complain that it knows the name (as you told it) but it doesn''t have a full declaration. Include the header you need for the AudioStream where it''s needed rather than in the rest of the project as well.

peace
Just follows some simple rules (might not recommended by others ) :

  //-----------------------// BadGuy.h#if !defined(BADGUY_H)#define BADGUY_H#include <fstream>#include <string>#include "GoodGuy.h"class GoodGuy;class BadGuy{  ...  void Hit(GoodGuy &gg);};#endif//-----------------------// GoodGuy.h#if !defined(GOODGUY_H)#define GOODGUY_H#include <fstream>#include <string>#include "BadGuy.h"class BadGuy;class GoodGuy{  void Hit(BadGuy &bg);  ...};#endif  
"after many years of singularity, i'm still searching on the event horizon"
Also you could try reading my article on this sort of thing - see ''Organizing Code Files'' in my signature. A lot of people seem to find it helpful.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost | Asking Questions | Organising code files | My stuff ]

This topic is closed to new replies.

Advertisement