Simple c++ question

Started by
4 comments, last by ToohrVyk 15 years, 5 months ago
I'm having a problem using cin. The code used to work perfectly but know it doesn't. string name; cout << "Please enter name: "; cin >> name; When compiling I get error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) This error started when I tried to overload the << operator to use one of my own classes with cout but I didn't touch this piece of code and have now commented this out in case it would cause any problems but it still doesn't work. I am including iostream and have using namespace std;
Advertisement
Quote:Original post by Android07
cin >> name;

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

You didn't introduce your own variable called "cin" by any chance?
The following works. Check your includes.
#include <string>#include <iostream>#include <istream>#include <ostream>int main(){  std::string name;  std::cout << "Please enter name: ";  std::cin >> name;}
You need to:

#include <string>

Since string is part of the std namespace, you must use:

using namespace std;

or

std::string

Thanks everyone, but it's working again now, I didn't have string.h included only iostream.h.

I didn't think that I needed to include both as the string type was recognised.

Thanks again.
Quote:Original post by Android07
Thanks everyone, but it's working again now, I didn't have string.h included only iostream.h.
These headers have been replaced by the standard headers <string> and <iostream> in 1998. (Yes, that's 10 years ago) You should consider moving on to the current versions.

This topic is closed to new replies.

Advertisement