using string in <iostream> and <string>

Aug 7, 2009 at 4:34pm
closed account (Ly59GNh0)
A rather simple question, but is...

1
2
3
4
5
6
7
8
#include <iostream>

int main ()
{
    std::string a("Hello world!");
    std::cout << a << std::endl;
    return 0;
}


different from...

1
2
3
4
5
6
7
8
9
#include <iostream>
#include <string>

int main ()
{
    std::string a("Hello world!");
    std::cout << a << std::endl;
    return 0;
}


?

And if not, why does the book I'm reading insist that I include both iostream and string, when just iostream seems to be working just fine?
Aug 7, 2009 at 4:38pm
You should always include the header files containing the definitions of the class that you are using because if the implementation of your standard library includes <string> in <iostream>, other compilers with different library implementations may not have this.
When you #include a standard header you are just typing an extra line with no other issues as headers are guarded.
So: include all the headers that you need
Aug 7, 2009 at 5:06pm
closed account (Ly59GNh0)
Ahh, so it's compiler specific? I just assumed iostream was the same for everyone.
Thanks, that cleared it up. :)
Topic archived. No new replies allowed.