PDA

Click to See Complete Forum and Search --> : .h files within .h files


sail3005
Feb 7th, 2001, 09:25 PM
say i make a .h file that contains a class that i made. If i include the iostream.h file within the .h class file, and within the .cpp program file, so that it has been included twice within the project. Will this cause errors or anything. And will it include it twice, and then make the size of the final project bigger than it needs to be?

Cybrg641
Feb 7th, 2001, 10:03 PM
I don't think it will cause a problem. But I would suggest keeping your function declarations and stuff like that in the .h file and the code in the .cpp file. I guess it doesn't make much difference but it is more organized that way.

sail3005
Feb 7th, 2001, 10:12 PM
ok thanks, i was thinking more that if i sent a .h file to a friend and they didn't look at it and included it twice. But if it won't cause a problem that is good.

Thanks for the info

Jop
Feb 8th, 2001, 02:06 PM
I think it does generate errors!!!

One should always use

#ifdef //If Defined

or

#ifndef //If NOT Defined


To prevent headers being included twice!

parksie
Feb 8th, 2001, 03:21 PM
#ifndef __FILE_H__ // change to your filename, including underscores
#define __FILE_H__

/* Code here */

#endif // __FILE_H__

HarryW
Feb 8th, 2001, 03:46 PM
Why all the extra underscores? In the books I've read they recommend FILE_H, without the extras on the ends. Is that a standard thing or just your preference?

parksie
Feb 8th, 2001, 03:48 PM
Mainly my preference, because if you notice, the usual preprocessor stuff has two underscores to start it, so anything small won't interfere with it.

It's by no means standard, but I've seen it used quite a bit, so that's what I've got.

Still, I suppose it's better than what ClassWizard generates :rolleyes:

sail3005
Feb 8th, 2001, 05:59 PM
thanks for the advice, one question though. This may be stupid but, where you say


/* Code here */


is that the code for the entier program/cpp file? or something else?

HarryW
Feb 8th, 2001, 06:51 PM
It's your header file code. At the beginning of your header file you write:

#ifndef FILE_H
#define FILE_H

then at the end of your header file you write

#endif

As you probably know, anything beginning with a # is a preprocessor instruction. Basically, if you haven't already included the file, then FILE_H (just an arbitrary keyword really) will be undefined. If the file has already been included at least once, then FILE_H will be defined and the preprocessor will skip over the code between the #ifndef and the #endif.

sail3005
Feb 8th, 2001, 09:54 PM
ok, i see. I didn't know you put the If in the header file. I thought you put it in the .cpp file.

Thanks a lot!