Sign in to follow this  
Samsonite

WTF you can't send a std::string to cout?

Recommended Posts

Samsonite    528
Am I reading right?
...error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)



bool CMyEngine::CheckFor(string value)
{
	string a;
	cin >> a;
	if(a == value)
	{
		return true;
	}
	else
	{
		return false;
	}
}


Thanks in advance for any answer, good or bad [smile]

Share this post


Link to post
Share on other sites
Mushu    1396
std::cout uses operator<<, so you'd need to do something like

std::cout << myString << std::endl;

operator>>, however, is defined by std::cin and the usage you've shown in the bottom example appears to be correct.

Share this post


Link to post
Share on other sites
Monder    993
Are you including iostream.h and string.h or iostream and string? You need to include the latter, the former are pre-standard header and should not be used.

Also using >> to get a string from cin will only read a single word so if you do cin >> foo and you type in "la la la" foo will just contain "la".

Share this post


Link to post
Share on other sites
Max_Payne    757
I remember that there was such a problem in VC6 with cout, it was fixed in .NET.

If you're using .NET, my guess would be that it doesn't accept it with cin because you can't specify a maximum string length just by passing a string. In that case you would need to use getline() or another such function.

Share this post


Link to post
Share on other sites
Samsonite    528
Quote:
Original post by Monder
Are you including... and <string>?


There's always an easy answer...Thanks, and may I never forget to include <string> again...[smile].

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this