asyncwait
asyncwait

Reputation: 4537

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

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?

Upvotes: 40

Views: 107755

Answers (4)

Nick Delbar
Nick Delbar

Reputation: 121

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.

Upvotes: 1

Guy Avraham
Guy Avraham

Reputation: 3690

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

Upvotes: 2

Akshat Bhatt
Akshat Bhatt

Reputation: 11

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.

Upvotes: 1

sbi
sbi

Reputation: 224159

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.

Upvotes: 103

Related Questions