36

Please don't confuse with the title as it was already asked by someone but for a different context

The below code in Visual C++ Compiler (VS2008) does not get compiled, instead it throws this exception:

std::ifstream input (fileName);   

while (input) {
  string s;
  input >> s;
  std::cout << s << std::endl;
};

But this code compiles fine in cygwin g++. Any thoughts?

tmlen
  • 7,586
  • 3
  • 28
  • 65
asyncwait
  • 4,345
  • 4
  • 35
  • 51

4 Answers4

93

Have you included all of the following headers?

  • <fstream>
  • <istream>
  • <iostream>
  • <string>

My guess is you forgot <string>.

On a side note: That should be std::cout and std::endl.

sbi
  • 204,536
  • 44
  • 236
  • 426
  • 2
    You're right .. I missed , don't you think this error message is totally misleading. I can't relate this error message with the fix you're mentioned. Very strange!! – asyncwait Oct 27 '09 at 15:24
  • 8
    @Vadi: Very likely `std::string` is defined in some other header you already included, but the operator is not. So the compiler accepts `string s;`, but not the invocation of the stream operator. – sbi Oct 27 '09 at 15:38
  • You dont have to specify `std::cout`. If you're running with `using namespace std;`, then just `cout` is enough. – Bran van der Meer Mar 06 '14 at 13:19
  • @BranvanderMeer: [Why is “using namespace std;” considered bad practice?](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice/1453605#1453605) – sbi Mar 06 '14 at 13:21
  • @sbi, do you mean by `defined` that in some header someone did something like `typedef someType string;` ? – Tahlil May 22 '14 at 10:57
  • Mine did show the same error. I found out that I was using the . When I switched to simply it worked. – Robert Lucian Chiriac May 22 '14 at 16:23
  • @Robert Understandably so. `` is the C standard library's string handling functions. – sbi May 22 '14 at 21:23
  • 1
    @sbi, I don't have that kind of purpose. I had the same problem too yesterday. I just wanted to know what do you mean when you said `very likely std::string is defined in some other header you already included` in your comment. I am a beginner in C/C++. So pardon me if it's a very silly question or the answer is too obvious. Thank you. – Tahlil May 23 '14 at 04:55
  • 4
    @Tahlil: The C++ standard does not specify which other headers a standard library header might or might not include, so vendors are free to do anything they want. (Since including lots of stuff is costly in terms of compilation time, sometimes they split headers into different parts that are included from different headers.) So it might happen that something you did not include is [defined or declared](http://stackoverflow.com/a/1410632/140719) in some header included internally. Does that answer your question? – sbi May 23 '14 at 08:54
  • 1
    Yes. I understand now. Thank you very much for the explanation :-) – Tahlil May 23 '14 at 10:05
  • 1
    perfect. you saved me. :) – Abhishek Goel Jun 23 '14 at 05:46
2

Adding to @sbi answer, in my case the difference was including <string> instead of <string.h> (under VS 2017).

See the following answer: similar case answer

Lightness Races in Orbit
  • 358,771
  • 68
  • 593
  • 989
Guy Avraham
  • 2,830
  • 2
  • 31
  • 43
1

In addition to what others said. The following code was necessary in my application to compile succesfully.

std::cout << s.c_str() << std::endl;

Another work-around to this is go to project properties -> General -> Character Set and choose "Ues Multi-Byte Character Set" (You won't need to use c_str() to output the string)

There's disadvantages to using MBCS so if you plan to localize your software, I'd advize against this.

Nick Delbar
  • 61
  • 1
  • 8
0

include <string>

Try including string header file along with <iostream> file. It will work in some compilers even without the <string> because settings for different compilers are different and it is the compiler that is responsible for reading the preprocessor files that start with '#' symbol to generate a obj file.