0
#include<iostream>
#include<string>

using namespace std;
void main(){
string str="abc";
cout<<str;
system("pause");
}

If i do not include string header file then there is an error at << in line cout<

I thought the error will be at line where str is defined.

CODError
  • 779
  • 2
  • 10
  • 25
  • 2
    Well, no. Compilers are automata, not humans. It's entirely understandable that they report an error *near* its actual location. –  Jan 05 '14 at 18:36
  • @H2CO3 but I think he refers to the initialization and wonders why it does not happen there... – Sebastian Jan 05 '14 at 18:37
  • 1
    @H2CO3 That was my interpretation as well. – Carey Gregory Jan 05 '14 at 18:37
  • 3
    `void main` is not legal. It must return `int`. Anyway, I've noticed MSVC used to be particularly fond of providing everything in `` except the I/O through ``, which is a bit ironic. – chris Jan 05 '14 at 18:39
  • @chris: The class itself comes through the exceptions which are required by the constructor of [`ios_base::failure`](http://en.cppreference.com/w/cpp/io/ios_base/failure) (and perhaps some others). But I don't think other unneeded non-member functions, such as `to_string` are brought in. – Benjamin Lindley Jan 05 '14 at 18:54
  • @BenjaminLindley, Ah, I've just seen too many duplicate questions of "why can I use `string`, but not output it?" – chris Jan 05 '14 at 18:57

1 Answers1

10

Standard library headers can include other standard library headers, even if not specified in the standard. So it may be that with your implementation, the iostream header includes some parts of the string header, so that std::string is available but std::operator<<(std::basic_ostream<...>&, const std::basic_string<...>&); is not.

Joseph Mansfield
  • 108,238
  • 20
  • 242
  • 324