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.

6
  • 2
    Well, no. Compilers are automata, not humans. It's entirely understandable that they report an error near its actual location. Commented Jan 5, 2014 at 18:36
  • @H2CO3 but I think he refers to the initialization and wonders why it does not happen there... Commented Jan 5, 2014 at 18:37
  • 1
    @H2CO3 That was my interpretation as well. Commented Jan 5, 2014 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 <string> except the I/O through <iostream>, which is a bit ironic. Commented Jan 5, 2014 at 18:39
  • @chris: The class itself comes through the exceptions which are required by the constructor of ios_base::failure (and perhaps some others). But I don't think other unneeded non-member functions, such as to_string are brought in. Commented Jan 5, 2014 at 18:54

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.