Error: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' [duplicate]

StackOverflow https://stackoverflow.com/questions/19872314

My Question is similar to others but I wasn't able to find and answer that quite fit, maybe I'm just missing it, but anyways.

Given that this is at the top of my .cpp:

#include <cstring>

#include <iostream>

using namespace std

why would this line have an error:

cout << endl << output << endl;

the error being:

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

有帮助吗?

解决方案

<cstring> is the header for C strings, i.e, its content is the same as the C header string.h. What you need to handle std::string is <string>

Another problem is that you missed the semicolon:

using namespace std;
//                 ^

Note that this style works, but is not recommend, it's better not to use this line and use:

std::cout << std::endl << output << std::endl;
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top