[英]'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. LPWSTR
是wchar_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.