简体   繁体   English

“正在初始化”:无法从“const wchar_t[35]”转换为“LPWSTR”

[英]'Initializing': Cannot convert from 'const wchar_t[35]' to 'LPWSTR'

i am currently learning C++ and want to change my desktop wallpaper.我目前正在学习 C++ 并想更改我的桌面墙纸。 However i am getting this error above.但是我在上面收到了这个错误。

#include <string>
#include <iostream>
#include <Windows.h>

using namespace std; 

int main() {

LPWSTR test = L"C:\\Users\\user\\Pictures\\minion.png";

int result = SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, 
test, SPIF_UPDATEINIFILE);


}

A value of type "Const wchar_t*" cannot be used to initialize an entity of type LPWSTR “Const wchar_t*”类型的值不能用于初始化 LPWSTR 类型的实体

Any ideas?有任何想法吗?

Thanks谢谢

LPWSTR is an alias for wchar_t* , ie a pointer to a non-const character. LPWSTRwchar_t*的别名,即指向非常量字符的指针。

A string literal is a const array of characters, in your case a const wchar_t[35] .字符串文字是 const 字符数组,在您的情况下是const wchar_t[35] It decays into a pointer to a const character, pointing at the 1st character in the literal.它衰减为指向 const 字符的指针,指向文字中的第一个字符。

You can't assign a pointer-to-const to a pointer-to-non-const.您不能将指向 const 的指针分配给指向非 const 的指针。 That would allow writing access to read-only memory.这将允许对只读 memory 进行写访问。

Use LPCWSTR instead, which is an alias for const wchar_t* .请改用LPCWSTR ,它是const wchar_t*的别名。

LPCWSTR test = L"C:\\Users\\user\\Pictures\\minion.png"; 

The MSVC compiler is getting less and less permissive. MSVC 编译器变得越来越不宽容。 On the whole that's a good thing.总的来说这是一件好事。

L"C:\\Users\\user\\Pictures\\minion.png" is a literal of type const wchar_t[34] (the extra element is for the string terminator). L"C:\\Users\\user\\Pictures\\minion.png"const wchar_t[34]类型的文字(额外的元素用于字符串终止符)。 That decays to a const wchar_t* pointer in certain circumstances.在某些情况下,这会衰减const wchar_t*指针。

LPWSTR is not a const pointer type so compilation will fail on a standard C++ compiler. LPWSTR不是const指针类型,因此在标准 C++ 编译器上编译将失败。

The solution is to use the const pointer type LPCWSTR .解决方案是使用const指针类型LPCWSTR

Another way to resolve this compilation error is to set Conformance Mode to Default in the project Properties -> C/C++ -> Language.解决此编译错误的另一种方法是在项目 Properties -> C/C++ -> Language 中将 Conformance Mode 设置为 Default。 At least it worked in my VS2019 project.至少它在我的 VS2019 项目中有效。

暂无
暂无

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

相关问题 错误:C2664 'void RecursiveDelete(LPWSTR,LPWSTR)':无法将参数 2 从 'const wchar_t [12]' 转换为 'LPWSTR' - error : C2664 'void RecursiveDelete(LPWSTR,LPWSTR)': cannot convert argument 2 from 'const wchar_t [12]' to 'LPWSTR' 错误C2440:“正在初始化”:无法从“ const wchar_t [9]”转换为“ LPCSTR” - error C2440: 'initializing' : cannot convert from 'const wchar_t [9]' to 'LPCSTR' 无法将参数1从&#39;const char *&#39;转换为&#39;const wchar_t *&#39; - cannot convert argument 1 from 'const char *' to 'const wchar_t *' 无法从&#39;const jchar *&#39;转换为&#39;const wchar_t *&#39; - cannot convert from 'const jchar *' to 'const wchar_t *' 无法从&#39;const wchar_t *&#39;转换为&#39;_TCHAR *&#39; - Cannot convert from 'const wchar_t *' to '_TCHAR *' 添加新用户会出现错误“无法将类型为“const wchar_t”的值分配给类型为“LPWSTR”的实体” - Adding new user gives error "a value of type "const wchar_t" cannot be assigned to entity of type "LPWSTR"" 无法将“const char*”转换为“LPCWSTR {aka const wchar_t*}” - cannot convert 'const char*' to 'LPCWSTR {aka const wchar_t*}' 不能从&#39;ATL :: CStringT转换参数1 <wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t> &gt;&gt;&#39;到&#39;const char *&#39; - cannot convert argument 1 from 'ATL::CStringT<wchar_t,ATL::StrTraitATL<wchar_t,ATL::ChTraitsCRT<wchar_t>>>' to 'const char *' 将const wchar_t *转换为WCHAR * - Convert const wchar_t* into a WCHAR* 无法从 lpstr 转换为 wchar_t - Cannot convert from lpstr to wchar_t
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM