Thread: #ifndef problem

  1. #1
    Registered User
    Join Date
    Dec 2008
    Location
    California
    Posts
    37

    #ifndef problem

    I moved from DevC++ to Codeblocks and I've come up with this code when I added a header file.

    Code:
    //Engine.h
    #ifndef ENGINE_H_INCLUDED
    #define ENGINE_H_INCLUDED
    #endif // ENGINE_H_INCLUDED
    In DevC++, I'm just including the file by using #include "Engine.h". I searched in google and still I didn't understand it. I have an Engine class in Engine.h and the functions in Engine.cpp. Can anyone explain this to me.

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Are you asking what that Include Guard does?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What do you want explained, exactly? "#ifndef" stands for "if not defined".

  4. #4
    Registered User
    Join Date
    Dec 2008
    Location
    California
    Posts
    37
    Ok I get it now. I thought it's a replacement for the Include file thing. lol. Thanks for the link cpjust.

  5. #5
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    The hash (#) symbol signifies a "preprocessor directive", which basically they are not apart of the actual program. They are instructions for the compiler. #ifndef xxx tells the compiler only to continue if xxx is not defined (ifndef: if not defined). #define xxx tells the compiler to define xxx (optional value). Then you have your source code. Finally #endif tells the compiler that's the end of the if statement (just like C conditional statements - if, while, for have bracket start and end).

    The entire purpose is that the compiler will start out with xxx being undefined, then it will define it, then it won't process everything inbetween (basically the entire header file) again, so there is no duplication. It will do that for all header files like that preventing duplication.

    Edit: should have refreshed the tab when I came back to it.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  3. Class Include Problem (I Think)
    By gpr1me in forum C++ Programming
    Replies: 8
    Last Post: 03-21-2006, 12:47 PM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  5. half ADT (nested struct) problem...
    By CyC|OpS in forum C Programming
    Replies: 1
    Last Post: 10-26-2002, 08:37 AM