12

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 Answers2

16

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]

gimpf
  • 4,503
  • 1
  • 27
  • 40
  • What fools you is that the line declaring a `wstring` works. Including gets you enough of to be able to declare one, but not `operator<<` - just helpful enough to hurt, I suppose. – Kate Gregory Mar 15 '13 at 18:31
0

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.

Andrew
  • 5,839
  • 1
  • 51
  • 72