Archived

This topic is now archived and is closed to further replies.

code wont compile because header inclusion configuration is complicated

This topic is 5761 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

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

Share on other sites
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).

~Bolt

Share on other sites
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

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;

  #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

Share on other sites
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

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 14
• 12
• 9
• 12
• 37
• Forum Statistics

• Total Topics
631435
• Total Posts
3000057
×