ifndef

This is a discussion on ifndef within the C++ Programming forums, part of the General Programming Boards category; #ifndef TREE_NODE #include "TreeNode.h" #endif #ifndef SEARCH_TREE #include "SearchTree.h" #endif what does this code mean?...

  1. #1
    Registered User mayfda's Avatar
    Join Date
    Apr 2002
    Posts
    11

    ifndef

    #ifndef TREE_NODE
    #include "TreeNode.h"
    #endif

    #ifndef SEARCH_TREE
    #include "SearchTree.h"
    #endif

    what does this code mean?
    -mayfda-

  2. #2
    Registered User dizolve's Avatar
    Join Date
    Dec 2002
    Posts
    68
    This means that if TREE_NODE is not defined already, then include TreeNode.h, which will most likely #define TREE_NODE. It's to prevent including a file twice.

      __               &n bsp;      ___ & nbsp;       &nb sp;       &nbsp ;    
     /\ \  __    &nbs p;           /\_ \      &nbsp ;        & nbsp;     
     \_\ \/\_\  ____     _ __\//\ \    __  __&n bsp;    __   
     /'_` \/\ \/\_ ,`\  / __`\\ \ \  /\ \/\ \  /'__`\ 
    /\ \_\ \ \ \/_/  /_/\ \_\ \\_\ \_\ \ \_/ |/\  __/ 
    \ \___,_\ \_\/\____\ \____//\____\\ \___/ \ \____\
     \/__,_ /\/_/\/____/\/___/ \/____/ \/__/   \/____/
            &n bsp; I have a BAD figlet& nbsp;addiction.

  3. #3
    Registered User
    Join Date
    Dec 2002
    Posts
    103

    An added information

    #ifndef stands for "If Not Defined"

    C / C++ supports multiple declaration but single definition.

    Hence if you have a class definition or something like that in a headerfile, if you include it more than once directly or indirectly, the compiler would generate an error.

    #ifndef XYZ
    #define XYZ

    // your class definitions etc comes here

    #endif

    When the compiler reads it for the first time, XYZ is not defined, hence it would enter the conditional-preprocessor directive and within that we are defining XYZ. If it revisits this, any other time, XYZ is defined and hence would not enter the coditional-preprocessor directive, thus preventing multiple definition.
    Last edited by shiv_tech_quest; 01-12-2003 at 09:35 PM.
    Have a wonderful day.... and keep smiling... you look terrific that way
    signing off...
    shiv... as i know him

  4. #4
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    So, the purpose of this is...

    To make your code more modular. Say you have two or more files (modules) that need to include iostream.h. Each file can say "If iostream has not been included yet, then include it now."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. #ifndef explanation?
    By Zooker in forum C Programming
    Replies: 2
    Last Post: 02-12-2009, 05:59 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. Warning on an ifndef directive
    By DavidP in forum C++ Programming
    Replies: 2
    Last Post: 08-02-2007, 01:31 AM
  4. #ifndef
    By paperbox005 in forum C Programming
    Replies: 4
    Last Post: 09-24-2004, 07:25 PM
  5. #ifndef wrapper to reference dll
    By joeyzt in forum C++ Programming
    Replies: 2
    Last Post: 06-21-2004, 01:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21