error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)

94,861

Solution 1

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.

Solution 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

Solution 3

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.

Share:
94,861
asyncwait
Author by

asyncwait

Updated on October 25, 2020

Comments

  • asyncwait
    asyncwait over 3 years

    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?

  • asyncwait
    asyncwait over 14 years
    You're right .. I missed <string>, 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!!
  • sbi
    sbi over 14 years
    @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.
  • Bran van der Meer
    Bran van der Meer about 10 years
    You dont have to specify std::cout. If you're running with using namespace std;, then just cout is enough.
  • sbi
    sbi about 10 years
  • Tahlil
    Tahlil almost 10 years
    @sbi, do you mean by defined that in some header someone did something like typedef someType string; ?
  • Robert Lucian Chiriac
    Robert Lucian Chiriac almost 10 years
    Mine did show the same error. I found out that I was using the <cstring>. When I switched to simply <string> it worked.
  • sbi
    sbi almost 10 years
    @Robert Understandably so. <cstring> is the C standard library's string handling functions.
  • Tahlil
    Tahlil almost 10 years
    @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.
  • sbi
    sbi almost 10 years
    @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 in some header included internally. Does that answer your question?
  • Tahlil
    Tahlil almost 10 years
    Yes. I understand now. Thank you very much for the explanation :-)
  • Abhishek Goel
    Abhishek Goel almost 10 years
    perfect. you saved me. :)
  • Lightness Races in Orbit
    Lightness Races in Orbit over 5 years
    How does this answer differ from the accepted one posted nine years ago? Okay, it's more thorough. If that's your intention then great!
  • Lightness Races in Orbit
    Lightness Races in Orbit over 5 years
    Although this doesn't really have anything to do with compilers or settings, but with how the standard library impl is laid out
  • Stephen.W
    Stephen.W over 2 years
    got me too. What a crappy compiler error prompt! :-(