Nov 20, 2011 at 1:15am UTC
Hello
I have just learnt the purpose of a header file, but don't understand why they need a header guard. I also don't understand the format of the header guards.
#ifndef HEADERNAME_H
#define HEADERNAME_H
...
#endif
Firstly, why would someone need to check if the header file is already defined? Secondly, where does a header file become 'defined' previousy such that you need to check if it has been defined.
Lastly, is it 'more normal' for a good-sized program to have one, or many, header files?
Thanks
Nov 20, 2011 at 2:14am UTC
And you don't include a header twice as the compiler doesn't like to be given definitions for some things more than once.
1 2 3 4 5 6 7
class SomeClass
{
};
class SomeClass
{
};
results in vc++ whinging...
1>w:\source\explore\cplusplus_vc9\temp_test\temp_test.cpp(15) : error C2011: 'SomeClass' : 'class' type redefinition
1> w:\source\explore\cplusplus_vc9\temp_test\temp_test.cpp(11) : see declaration of 'SomeClass'
But you can forward declare a class as often as you like
1 2 3 4 5 6 7 8
class SomeClass;
class SomeClass;
class SomeClass;
class SomeClass;
class SomeClass
{
};
To find out more about #ifdef, #define, ... see
http://www.cplusplus.com/doc/tutorial/preprocessor/
Andy
P.S. I expect to have a similar number of headers to cpp files in a project. Sometimes a few more, if there are inline functions or inline only classes.
This is the set of files for a "grammar engine" DLL I'm working on (it uses rules and a dictionary of words to assemble sentences. Hence the parts of speech, etc.)
\GRAMENG\GRAMENG.CPP
\GRAMENG\GRAMENG.DEF
\GRAMENG\GRAMENG.H
\GRAMENG\grameng.project <- CodeLite project
\GRAMENG\GRAMENG.vcproj <- VC++ project
\GRAMENG\ReadMe.txt
\GRAMENG\StdAfx.cpp
\GRAMENG\StdAfx.h
\GRAMENG\Concepts\Action.cpp
\GRAMENG\Concepts\Action.h
\GRAMENG\Concepts\Concept.cpp
\GRAMENG\Concepts\Concept.h
\GRAMENG\Concepts\Entity.cpp
\GRAMENG\Concepts\Entity.h
\GRAMENG\FlashCards\BuildCard_NounPhrase.cpp
\GRAMENG\FlashCards\BuildCard_VerbPhrase.cpp
\GRAMENG\FlashCards\BuildCard_VerbPhrase_Pronoun.cpp
\GRAMENG\FlashCards\FlashCard.cpp
\GRAMENG\FlashCards\FlashCard.h
\GRAMENG\FlashCards\FlashCardFactory.cpp
\GRAMENG\FlashCards\FlashCardFactory.h
\GRAMENG\FlashCards\FlashCard_Concepts.cpp
\GRAMENG\Grammar\Clause.cpp
\GRAMENG\Grammar\Clause.h
\GRAMENG\Grammar\GrammarEngine.cpp
\GRAMENG\Grammar\GrammarEngine.h
\GRAMENG\Grammar\Phrase.cpp
\GRAMENG\Grammar\Phrase.h
\GRAMENG\Grammar\PhraseBuilder.cpp
\GRAMENG\Grammar\PhraseBuilder.h
\GRAMENG\Grammar\Sentence.cpp
\GRAMENG\Grammar\Sentence.h
\GRAMENG\Modules\GEXModule.cpp
\GRAMENG\Modules\GEXModule.h
\GRAMENG\Modules\Module.cpp
\GRAMENG\Modules\Module.h
\GRAMENG\PartsOfSpeech\Adjective.cpp
\GRAMENG\PartsOfSpeech\Adjective.h
\GRAMENG\PartsOfSpeech\Adverb.cpp
\GRAMENG\PartsOfSpeech\Adverb.h
\GRAMENG\PartsOfSpeech\Agreement.h
\GRAMENG\PartsOfSpeech\Article.cpp
\GRAMENG\PartsOfSpeech\Article.h
\GRAMENG\PartsOfSpeech\Noun.cpp
\GRAMENG\PartsOfSpeech\Noun.h
\GRAMENG\PartsOfSpeech\Pronoun.cpp
\GRAMENG\PartsOfSpeech\Pronoun.h
\GRAMENG\PartsOfSpeech\Verb.cpp
\GRAMENG\PartsOfSpeech\Verb.h
\GRAMENG\PartsOfSpeech\Word.cpp
\GRAMENG\PartsOfSpeech\Word.h
\GRAMENG\Utils\Utils.cpp
\GRAMENG\Utils\Utils.h
Last edited on Nov 20, 2011 at 2:24am UTC
Nov 20, 2011 at 3:07am UTC
Thanks, both those answers have been very helpful!