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

2

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.

Umbilication answered 17/9, 2009 at 13:51 Comment(0)
F
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]

Fey answered 17/9, 2009 at 13:59 Comment(1)
What fools you is that the line declaring a wstring works. Including <iostream> gets you enough of <string> to be able to declare one, but not operator<< - just helpful enough to hurt, I suppose.Kakemono
H
0

For those with this problem, you may need to enable multi-byte printing in the console. See the answer here: https://mcmap.net/q/240188/-how-to-initialize-and-print-a-std-wstring

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.

Handmedown answered 14/4, 2018 at 22:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.