Thread: Header file (multiple inclusions)

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    38

    Header file (multiple inclusions)

    I get the impression (from previous posts) that when working with large numbers of header files, the coder should always include header "guards" to keep them from being included more then once. Header guards being the ("ifndef HEADER_H ....stuff). This is something that I have not been doing, and I am constantly getting redefinition errors.
    Code:
    #ifndef __yourheader_h__
    #define __yourheader_h__
    
    //all your code goes here
    
    #endif
    A few questions about this:
    (1) Is this the best way to avoid having classes included multiple times (thus causing errors)?
    (2) Is this techinique "Good Coding Practice" or is it a "work around"...ie are you guru's doing this too?

    Formating questions:
    (1) if the header file that I am creating also needs to "#include" other classes, does it matter if I do that before the "#ifndef" guard statement or after?
    (2) Does the name that I used in the #ifndef _HEADERNAME_H matter (ie. do I have to use underscore, or can I just use the class name?)
    (3) I have also seen this technique (below) online...when/how (if ever) should I use this?
    Code:
    #ifndef _HEADERFILE_H
    #include "HeaderFile.h"
    #endif
    Thanks for your help
    Last edited by cjschw; 08-10-2004 at 09:54 AM.
    chris

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Encryption program
    By zeiffelz in forum C Programming
    Replies: 1
    Last Post: 06-15-2005, 03:39 AM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM