Skip to main content
gamedev.net
🔒 Locked

Header file confusion

Started by david williams Sep 25, 2000 at 3:41 PM 1 replies 250+ views
Original Post
david williams
david williams
I am trying to make my 3D engine as modular as possible, but I am having problems including header files correctly. I have a file called basics.h which contains classes like CVertex, CColour, and CVertex, and the implementation is in basics.cpp. I the have files called world.h and world.cpp which have classes like CWall, CSector and so on, and these types contain some of those defined in basics.h. This means both world.h and world.cpp need acsess to basics.h. Should it be included in both files? Is it OK to include headers from other headers? what if I now have my main program in main.cpp and main.h. Do i need to include world.h in both? (main.h may have references to the classes in function prototypes) Do I also need to include basics.h or is it automatilly included via world.h/world.cpp? You get the idea, what should be in header files and how should they all link together?? Please help
JonStelly
JonStelly
It is acceptable to include header files in other header files. Inheritance is one example. What you are describing is another.



/*
// basics.h
*/

class CVertex
{
...
}

class CColor
{
...
}


/*
// world.h
*/

#include "basics.h"

class CWall
{
...
CVertex m_Verts[4];
CColor m_Color;
}


/*
// Main.cpp
*/

#include "World.h"

int main(...)
{
CWall wall;
CColor c(1.0, 1.0, 1.0);
wall.Set(c);
}

risingdragon3
risingdragon3
...But don''t forget the #ifndef / #endif ''s:
    
#ifndef _BASIC_H_
#define _BASIC_H_

//header


#endif


or else if you include more then once you will get lots of ''redefined''/''redeclared'' errors.



------------------------------
What should I put here? You tell me.

Topic Locked

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

Sign in to reply to this topic.