Error C2679 when attempting to use std::wcout << wstring-var; vc++ 2008 express

4.3k Views Asked by At

I'm getting a rather odd error message when attempting to wcout a wstring in vc++ 2008 express:

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

If I understand this correctly it's reporting that wcout does not accept a wstring? I ask someone to compile this code under linux and it runs fine. I also tried the same code on another computer with vc++ 2008 express and still fails. A known issue with std in vc++ 2008?

#include <iostream>

int main()
{
 std::wstring unicode_test = L"Unicode var";
 std::wcout << L"Unicode non-var" << std::endl;
 std::wcout << unicode_test << std::endl;    //<-- This line fails!
}

I'm using vc++ 2008 express sp1 with all the updates up to KB948127. I'm aware that console will need codepage changes but this isn't even compiling. Thanks.

2

There are 2 best solutions below

1
On BEST ANSWER

You need to #include <string>. I'm not sure what the standard says, but I'm quite sure that <iostream> is not required to export all of <string>, if any.

[edit]At least cplusplus.com does not even list string as the types declared in <iostream>. No, it's not the standard, I know...[/edit]

0
On

For those with this problem, you may need to enable multi-byte printing in the console. See the answer here: https://stackoverflow.com/a/41584090/1599699

And my comment:

I was having trouble printing a wstring that I instantiated with a greater length than the data I was supplying due to sizeof(wchar_t) == sizeof(char) * 2, and then printing anything after that wasn't succeeding.