views:

85

answers:

2

I am trying to compile the following code in my test application on windows in visual studio for C++:

const wchar_t* chinese = "好久不见";

But I get the following error:

error C2440: 'initializing' : cannot convert from 'const char [5]' to 'const wchar_t *

I am compiling with unicode, so I am confused about this. The error goes away if I cast the literal like this:

const wchar_t* chinese = (wchar_t*)"好久不见";

I am not sure that is safe nor do I really want to do that so how can I fix this.

Thank you!

+11  A: 

You want a wide string literal, so prefix the string literal with L:

const wchar_t* chinese =  L"好久不见";
GMan
A: 

Like GMan said, prefix the string with an 'L'. The L identifies the string as a wide char type (wchar_t).

const wchar_t* chinese = L"好久不见";

Zain