Thread: to use .h or not to use .h that is the question

  1. #1
    samurai warrior nextus's Avatar
    Join Date
    Nov 2001
    Posts
    196

    Question to use .h or not to use .h that is the question

    ok. in my c++ programmming class in highschool we use visual c++ 6.0 pro. and we use this book that comes with its own library. we added it to our library of header files.

    well when i type code like this
    Code:
    #include <iostream>
    #include <string.h>
    
    int main()
    {
    String happy = "happy";
    std::cout << happy;
    
    return 0;
    }
    it gives me an error and say "cout" is ambigous. but when i write code like this:

    Code:
    #include <iostream.h>
    #include <string.h>
    
    int main()
    {
    String happy = "happy";
    std::cout << happy;
    
    return 0;
    }
    it doesnt give me an error. i know the new standard is without the .h because of namespaces, etc....can anyone help and explain to me why...also i cant do #include <string> it says file not found...why do i have to put a .h for string but not iostream?

    thanks

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Okay so I'll address this the best I can without being overly specific.

    File: string.h

    This file contains common string functions such as strlen, strcpy, strcat, etc. Even has memory stuff like memmove, memcpy, etc.

    File: string ( no extension )

    This is the templated string class. This is what you are using in your program. Note is should also be lower-case string not String.

    File: cstring ( no extension )

    This is the new ANSI standard headers. They prefix many headers with a C and strip off the h. Basically this file turns around and includes string.h. So for your program I suggest doing this.

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main( int argc, char *argv[] )
    {
      string happy = "happy";
      cout << happy << endl;
    
      return 0;
    }

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    The C++ commitee basically thought it'd be really cool to type something like <fullosteam> rather than <full_of_hot_air.h>, so they decided to drop the file extension. Don't know why you are having that anomolous behaviour, but anyway, if you look in your headers you will in fact see no extension on the C++ header files, but you will for the C style ones. Hence the "file not found".
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    160
    Could one risk to get another file if using #include string instead of #include string.h or is it exactly the same file the compiler will look for - that is if it is smart enough to add ".h"
    Well english isn't my first language, (it's instead a useless language called danish which only 5 milion people speak!!) so if you think my grammar SUCKS (it does by the way) than you're more then welcome to correct me.
    Hell I might even learn something

  5. #5
    Registered User Xaviar Khan's Avatar
    Join Date
    Sep 2001
    Posts
    10

    Thumbs up Namespace errors.

    Nextus,

    Typing 'using namespace std;' before your main function will work fine for most anything you will probably need to use. It's good practice imho to put this though.

    Code:
    using std::cout;
    using std::cin;
    using std::endl;
    As soon as you hit the 2 colons, a list will come up saying what you can use. This substitutes the 'std::' before every cout line.

    hth
    X
    Xaviar Khan
    Programmer/Owner
    Shards of Destiny (SoD)
    ---------------------------------------
    Reach us with any MUD client at ShardsofDestiny.org
    port 9000.
    +------------------------------------+
    | Html/Java support added, |
    | 'www.ShardsofDestiny.org' |
    +------------------------------------+

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. Question about no '.h' standard
    By JLBSchreck in forum C++ Programming
    Replies: 1
    Last Post: 05-07-2003, 02:26 PM
  3. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  4. placement of (.h) and (.cpp) files..!?
    By matheo917 in forum C++ Programming
    Replies: 3
    Last Post: 02-22-2003, 06:37 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM