wtf visual studio

Started by
9 comments, last by eedok 17 years, 9 months ago
for some reason this won't compile

#include <iostream>
using namespace std;

int main()
{
	int t=0;
	string str = "lol";
	while (t==0)
	{
		cout<<str<<endl;
		cin >> t;
	}
}




when I hit the almighty compile button I'm getting this: c:\Documents and Settings\cody\Desktop\test\hello.cpp(10): error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) umm well I can't find any problems in my code so maybe someone else could help me? EDIT: Forgot to add I'm using VS.NET 2003 standard oddly enough this works:

#include <iostream>
using namespace std;

int main()
{
	int t=0;
	string str = "lol";
	while (t==0)
	{
		cout<<str.c_str()<<endl;
		cin >> t;
	}
}


Advertisement
I'm a n00b, but it seems to me that you are mixing c and c++ functions. Try the c++ string printer istead of cout.

or use char[4] = "lol\0" instead of string.
#include <string> [wink]
Quote:Original post by Drew_Benton
#include <string> [wink]

thanks that fixes it, odd how the 2nd example works though
That's because iostream declares the operator<<(std::ostream, const char *) overload, but does not declare the operator<<(std::ostream, const std::string &) overload. To get that, you need to include <string>.
Quote:Original post by eedok
thanks that fixes it, odd how the 2nd example works though

Actually, I think it's odd that it works at all without the <string> include. Otherwise, the second version works because cout expects a char array, which is what string::c_str() returns.
here's another strange encounter this line of code gives me a couple errors as well:
std::transform(myString.begin(), myString.end(), myString.begin(), std::toupper);

'toupper' : is not a member of 'std'
'transform' : is not a member of 'std'

oddly enough they came up in the autocomplete, so I'm pretty sure they're a member of std.
Did you include <algorithm> and <cctype>?
Quote:Original post by SiCrane
Did you include <algorithm> and <cctype>?


nope, is there a site saying what you have to include to make things work for STL stuff, as it's looking like I'm having some issues with it.
Quote:Original post by eedok
is there a site saying what you have to include to make things work for STL stuff, as it's looking like I'm having some issues with it.


MSDN has lots of information on the STL, such as necessary include files.

Also, just a general tip. Problems like these are almost never the compiler or the IDE's fault, which is to say they are the programmers. Making a topic with a title like "wtf visual studio" leads me to believe, at first glance, that you have a problem for which you blame your IDE. IMO one of the biggest mistakes a beginner can make is to blame his compiler for his own mistakes (not to imply that you do this, just a general observation as I have dealt with people who do this far too often). I'm not trying to be the least bit malicious, I just suggest to you may find it helpfull in the future to approach problems from the perspective of "what did I do wrong?" rather than "why is my IDE broken?".

This topic is closed to new replies.

Advertisement