I cannot initializate WCHAR

*爱你&永不变心* 提交于 2020-01-17 16:38:28

问题


I need to make and WCHAR. But it wont work, and i always get an error:

Error   C2440   'initializing': cannot convert from 'const wchar_t [11]' to 'WCHAR *'
 StateError (active)    E0144   a value of type "const wchar_t *" cannot be used to initialize an entity of type "WCHAR *

My code:

WCHAR *Testlooll = L"TEST";

回答1:


L"TEST" is a string literal of type const wchar_t[5], which is an array of const characters (since the literal exists in read-only memory). You are trying to initialize a WCHAR*, which is a pointer to a non-const character, to point at that array.

Initializing a pointer to non-const character data to point at an array of const character data is deprecated in C++98 (to maintain backwards compatibility with legacy code), and is illegal in C++11 onwards.

You need to change the declaration of Testlooll according:

const WCHAR *Testlooll = L"TEST";

Or:

LPCWSTR Testlooll = L"TEST";


来源:https://stackoverflow.com/questions/52137990/i-cannot-initializate-wchar

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!