简体   繁体   English

如何在Visual Studio(C ++)中将文字汉字字符串分配给wchar_t *?

[英]How do I assign a literal chinese string to a wchar_t* in visual studio(c++)?

I am trying to compile the following code in my test application on windows in visual studio for C++: 我正在尝试在Visual Studio for Windows的Windows上的测试应用程序中编译以下代码:

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

But I get the following error: 但是我收到以下错误:

error C2440: 'initializing' : cannot convert from 'const char [5]' to 'const wchar_t * 错误C2440:“正在初始化”:无法从“ const char [5]”转换为“ const wchar_t *

I am compiling with unicode, so I am confused about this. 我正在使用unicode进行编译,因此对此感到困惑。 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! 谢谢!

您需要宽字符串文字,因此请在字符串文字前加上L

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

Like GMan said, prefix the string with an 'L'. 就像GMan所说的那样,在字符串前加上'L'。 The L identifies the string as a wide char type (wchar_t). L将字符串标识为宽字符类型(wchar_t)。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM