Thread: include library header in header files

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    133

    include library header in header files

    I was working with xemacs in unix. One of my c++ class header file has a string variable. E.g.:

    document.h:
    Code:
    #ifndef DOC_H
    #define DOC_H
    
    #include <string>
    using std::string;
    
    class Document
    {
    public:
    /* ... */ 
    private:
    String name;
    };
    
    #endif
    Notice the #include <string>. I had to place that or else the compiler doesnt recognize the string variable. Why is it so? I never had to do that in VC++. Can't i just include the string header before this header file in the implementation file?

    E.g:
    document.cpp:

    Code:
    #include <iostream>
    #include <string>
    #include "document.h"
    
    using namespace std;
    
    /*.........Implementations of class document....*/

  2. #2
    Registered User
    Join Date
    Jul 2004
    Posts
    19
    When i use a string the same way you described, i had to use the using-stament the same way in microsoft VC 6 too. But i must warn you, never to use a using-stament in header-files. Its a real bad habit, because you force every user of your header/lib to live together with your using-statement. Its much better to always use the complete "path" in your header, for example std::string, std::vector an so on. Use using-statments only in cpp-files.

    mfg JJ

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    133
    Yes thanks, i am aware of that namespace danger. My program was just a small application, so i was lazy to specify the namespace. But it's really strange. I never had to include a library header in my own header files before. Really wish someone could tell me why.

  4. #4
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,090
    Sometimes some library headers include other library headers inside them. However, this depends on each library implementation and cannot and should not be counted on.

    One example is in VC++ 6.0, <iostream> includes portions of <string>. Unfortunately, this leads people to believe that they don't need the <string> header to use std::string, and later on they get errors trying to output the string without using c_str(), among other potential problems.

    So you should include the header file that contains the declarations you need even if it works without it.

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    133
    document.cpp:
    Code:
    #include <iostream>
    #include <string>
    #include "document.h"
    
    using namespace std;
    
    /*.........Implementations of class document....*/
    I included it in document.cpp. But my question is why must i include it in the class header "document.h" in order for it to recognize string variables.

  6. #6
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by Raison
    document.cpp:
    Code:
    #include <iostream>
    #include <string>
    #include "document.h"
    
    using namespace std;
    
    /*.........Implementations of class document....*/
    I included it in document.cpp. But my question is why must i include it in the class header "document.h" in order for it to recognize string variables.
    When the compiler reads document.h, it doesn't know about <string>

    Each file is treated separately by the compiler (preprocessor, in this case) and only knows what you tell it in that file. So in document.h, if you tell it to #include <string>, now it knows everything in <string> and all is OK, otherwise: no joy.

    Regards,


    Dave
    Last edited by Dave Evans; 09-26-2004 at 01:08 PM.

  7. #7
    Registered User
    Join Date
    Sep 2003
    Posts
    133
    I always tot the compiler knows how to link them together. Guess I will start including headers inside the headers from now on

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. where to include header files?
    By kaos_frack in forum C++ Programming
    Replies: 6
    Last Post: 12-30-2008, 03:09 PM
  2. uniting all .h files in an include folder
    By afflictedd2 in forum C++ Programming
    Replies: 8
    Last Post: 11-26-2008, 01:36 PM
  3. Header Files
    By Volair in forum C Programming
    Replies: 2
    Last Post: 12-09-2005, 10:51 AM
  4. to #include or not to #include
    By krygen in forum C++ Programming
    Replies: 9
    Last Post: 12-25-2004, 12:06 AM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM