why not work

Started by
11 comments, last by Replicon 18 years, 10 months ago
#include <iostream> main(){ std::string source = "Hello"; std::cout << source <<std::endl; } error:C2679
Advertisement
You need #include <string> to use std::string.
Ra
Quote:Original post by Ra
You need #include <string> to use std::string.


And std::cout doesn't take std::strings. You have to change the cout line to this:

std::cout << source.c_str() <<std::endl;
According to MSDN - assuming MSVC - error:C2679 means:

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

This code has two binary operators: = and << std::string overloads both of them so as far as I know, this code should work. You're not using MSVC 5.0 or anything are you?

This is not you're problem but you should declare int main( ) and have it return 0 at the end.
Quote:Original post by CTar
Quote:Original post by Ra
You need #include <string> to use std::string.


And std::cout doesn't take std::strings. You have to change the cout line to this:

std::cout << source.c_str() <<std::endl;


Yes it does.

In time the project grows, the ignorance of its devs it shows, with many a convoluted function, it plunges into deep compunction, the price of failure is high, Washu's mirth is nigh.

Quote:Original post by CTar
Quote:Original post by Ra
You need #include <string> to use std::string.


And std::cout doesn't take std::strings. You have to change the cout line to this:

std::cout << source.c_str() <<std::endl;

It accepts std::strings. There's no need to use c_str().

Ra
Sorry, I was almost sure I have had a problem before with using std::strings and std::cout together, but then it must have been something else, or my memory is just bad.
int main(void) {
// ...blahblah
return 0;
}

C++ doesn't like the old C-style "main() {}" with no return value.
The return value is not required. The standard declares that a main function which terminates without a return statement implicitly returns zero.

Enigma
Why did you start a second thread for this question?

This topic is closed to new replies.

Advertisement