Why do I have to explicitly include the header file in a code?

This is a discussion on Why do I have to explicitly include the header file in a code? within the C++ Programming forums, part of the General Programming Boards category; Here is the code Code: //vector.h int add(int x, int y); //vector.cc #include <iostream> int add(int x, int y){ return ...

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    284

    Why do I have to explicitly include the header file in a code?

    Here is the code
    Code:
    //vector.h
    int add(int x, int y);
    
    //vector.cc
    #include <iostream>
    
    int add(int x, int y){
      return (x+y);
    }
    
    int main(){
        std::cout<<add(1,1)<<std::endl;
        return 0;
    }
    Obviously I do NOT need to include vector.h to compile. However, when I use namespace:

    Code:
    //vector.h
    namespace TEST{
      int add(int x, int y);
    }
    
    
    //vector.cc
    #include <iostream>
    #include "vector.h"
    
    int TEST::add(int x, int y){
      return (x+y);
    }
    
    int main(){
        std::cout<<TEST::add(1,1)<<std::endl;
        return 0;
    }
    I have to include "vector.h" otherwise compiler complains:
    Code:
    > g++ -o vec vector.cc
    vector.cc:5: error: syntax error before `::' token
    vector.cc: In function `int main()':
    vector.cc:10: error: `TEST' undeclared (first use this function)
    vector.cc:10: error: (Each undeclared identifier is reported only once for each 
       function it appears in.)
    vector.cc:10: error: parse error before `::' token
    Anybody knows why is that?

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,622
    All functions and objects need to be explicitly listed in a namespace somewhere in order to be compiled correctly, that's the difference.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    The compiler can't magically see functions in C++. So if you try to call a function that is not defined in the current file (or it's defined BELOW the function you're calling it from), then you must inform the compiler that the function will be there later.
    This is done using prototypes, which are typically placed in header files.

    Your first example works because you define the function before the call.
    The second fails because the compiler doesn't know of any TEST namespace (you haven't created one, and especially not placed a function inside it).

    However, you can do:
    Code:
    namespace TEST
    {
        int add(int x, int y)
        {
            return (x+y);
        }
    }
    And it will compile without the header (because you are telling the compiler to create the namespace and inside it, the function is defined).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,157
    Quote Originally Posted by Elysia View Post
    The compiler can't magically see functions in C++. So if you try to call a function that is not defined in the current file (or it's defined BELOW the function you're calling it from), then you must inform the compiler that the function will be there later.
    This is done using prototypes, which are typically placed in header files.
    What you say here is correct, except that you have used the word "defined" when the correct word to use was "declared".

    A function prototype is a declaration, but it is not a definition.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    I was referring to the actual definition and not the declaration, though.
    If a function is defined above the function that calls it, no prototype is necessary.
    If it's below or is another file, a prototype or declaration is necessary.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,230
    Quote Originally Posted by Elysia View Post
    I was referring to the actual definition and not the declaration, though.
    If a function is defined above the function that calls it, no prototype is necessary.
    If it's below or is another file, a prototype or declaration is necessary.
    This is because a definition is a declaration, although the converse is not true. A declaration for a function must exist at the time it is referenced. This declaration could come from a prototype, or from a previous definition of the function. If a function is defined, and a previous declaration exists (i.e. a prototype from a header file), the declarations must match each other.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unable to open include file SDL_audio.h
    By rraj.be in forum C Programming
    Replies: 2
    Last Post: 06-28-2008, 08:04 PM
  2. C programing doubt
    By sivasankari in forum C Programming
    Replies: 2
    Last Post: 04-29-2008, 09:19 AM
  3. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  4. Replies: 6
    Last Post: 04-02-2002, 04:46 AM

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