"LPVOID"不能分配给 X 类型的实体

"LPVOID" can not be assigned to an entity of the type X

本文关键字:类型 实体 分配 LPVOID 不能 不能分      更新时间:2023-10-16

我正在处理一个处理USB设备的函数。

但是我已经在一些简单的事情上遇到了问题:

我得到编译错误

A value of the type "LPVOID" can not be assigned to an entity of the type "PSP_DEVICE_INTERFACE_DETAIL_DATA" in the line 
"DevIntfDetailData = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwSize);"
谁能告诉我我在这里做错了什么?谢谢你!
PSP_DEVICE_INTERFACE_DETAIL_DATA DevIntfDetailData;
DevIntfDetailData = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwSize);

必须工作:

DevIntfDetailData = reinterpret_cast<PSP_DEVICE_INTERFACE_DETAIL_DATA>(HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, dwSize));

HealAlloc(像所有其他alloc函数一样)返回指向内存堆(void*)的指针,并且c++不允许将类型T*的值设置为类型void*的值而不手动强制转换。

存在dynamic_cast、static_cast、reinterpret_cast和const_cast。当你想要将void*转换为任何指针时,你必须使用reinterpret_cast,因为它转换类型时不需要进行任何检查(将void*转换为任何T*不会通过任何检查)。

相关文章: