Sign in to follow this  
Onemind

Common Header Questions

Recommended Posts

Q: Why do I have to use header files in C++? A: Technically, you can put all your code into the .h file, but that is consider very bad coding. When C++ was created, computers didn't have the RAM or the processing power they have now, so the C++ compiler doesn't do as much "housekeeping" as more modern languages. The header files were created to help the compiler figure out how to put the various CPP files together without shooting itself in the foot. Q: I get an error saying that there are multiple definitions of my class (or variable or function). Alternatively, you may get an error saying that you have recursive (or nested) includes. A: You may have included the same header more than once in a file. Most likely, you have included one header file in another header file, and included both of them in a CPP file.
// this is myheader.h 
class A
{
...
};


// this is mynewheader.h
#include "myheader.h"  // notice I've included the above file

class B
{
};


// this is myCPP.cpp file
#include "myheader.h"     // I've included myheader.h

// I've included a file  that includes
// myheader.h, so the compiler thinks it should be included 
// twice!
#include "mynewheader.h"  

To fix this problem, you need to add "code guards". You can use some compiler commands called compiler directives to make sure every include file is "included" only once - thus preventing your code from being included multiple times. You should do this in every .h file.
// during compilation, this will ask the compiler
// if MYFILE_H is not defined defined 
// this is like an if statement for the compiler
#ifndef MYFILE_H  


// if we get here, it means it is not defined, so define it
// you don't need to give it a value 
#define MYFILE_H

...

// if it was already defined, then the #ifndef will jump
// the compiler over the code to this line, thus
// not including any of the data!
#endif

Share this post


Link to post
Share on other sites
Quote:
Original post by raptorstrike
hold on did you just answer your own question in the same post? kinda pointless to post it if you ask me


Good thing noone did.

I'm sure some newbie will find this post useful. Not everyone knows to use #ifndef to keep from including header files more than once.

Share this post


Link to post
Share on other sites
Quote:
Posted by ontheheap
I'm sure some newbie will find this post useful. Not everyone knows to use #ifndef to keep from including header files more than once.


Thats the long & short of it. I see that question come up at least once a week, so instead of retyping all that I decided to just make a 1 post about it. I forgot about Kylotan's guide - oops.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this