Why do i have to include string header?

This is a discussion on Why do i have to include string header? within the C++ Programming forums, part of the General Programming Boards category; Because this works just fine: Code: #include <iostream> using namespace std; int main(){ string SayHello = "Hello"; cout<<SayHello<<endl; return 0; ...

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    4

    Why do i have to include string header?

    Because this works just fine:
    Code:
    #include <iostream>
    using namespace std;
    int main(){
        string SayHello = "Hello";
        cout<<SayHello<<endl;
        return 0;
    }

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It is completely implementation-dependent which standard headers include other standard headers. Therefore this code may or may not compile, depending on the standard library implementation.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    4
    It is completely implementation-dependent which standard headers include other standard headers.
    Does it mean that <iostream> header include <string>??

  4. #4
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    It tottaly depends on u dude ,,,,
    actually std is a namespace so when u r saying that using namsepsace std; means what ever will be in that namespace will be available to u.....

    it depends on u if u donu wanna that

    code like this

    Code:
    #include <iostream>
    
    int main() {
        string SayHello = "Hello";
        std::cout << SayHello << std::endl;
    
        return 0;
    }

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Yes, some <iostream> implementations include <string> (probably indirectly), while others perhaps don't include it.

    For example, your code compiles with GCC and Comeau Online, but not with MSVC. If you want your code to compile the same with all compilers, you'll need to include all the headers you need, even if with some compilers some of these inclusions are redundant.

    using namespace std; is irrelevant here, because the std namespace will only contain those things at that point which have been declared in the included headers. If you don't include <vector> (either directly or through some other header), that won't be enough to make std::vector visible. (Namespaces are open: in each header they are "opened up" again, and more stuff is declared to be part of it.)
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 01:02 AM
  2. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  3. Headers that use each other
    By nickname_changed in forum C++ Programming
    Replies: 7
    Last Post: 10-03-2003, 04:25 AM
  4. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 10:06 PM

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