error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
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.
asyncwait
Updated on October 25, 2020Comments
-
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 over 14 yearsYou'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 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 acceptsstring s;
, but not the invocation of the stream operator. -
Bran van der Meer about 10 yearsYou dont have to specify
std::cout
. If you're running withusing namespace std;
, then justcout
is enough. -
sbi about 10 years@BranvanderMeer: Why is “using namespace std;” considered bad practice?
-
Tahlil almost 10 years@sbi, do you mean by
defined
that in some header someone did something liketypedef someType string;
? -
Robert Lucian Chiriac almost 10 yearsMine did show the same error. I found out that I was using the <cstring>. When I switched to simply <string> it worked.
-
sbi almost 10 years@Robert Understandably so.
<cstring>
is the C standard library's string handling functions. -
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 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 almost 10 yearsYes. I understand now. Thank you very much for the explanation :-)
-
Abhishek Goel almost 10 yearsperfect. you saved me. :)
-
Lightness Races in Orbit over 5 yearsHow 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 over 5 yearsAlthough this doesn't really have anything to do with compilers or settings, but with how the standard library impl is laid out
-
Stephen.W over 2 yearsgot me too. What a crappy compiler error prompt! :-(