从“void *”到“unsigned char *”的转换无效 [英] Invalid conversion from ‘void*’ to ‘unsigned char*’
本文介绍了从“void *”到“unsigned char *”的转换无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码:
void* buffer = operator new(100);
unsigned char* etherhead = buffer;
我在尝试编译时遇到以下错误:
I'm getting the following error for that line when trying to compile;
error: invalid conversion from ‘void*’ to ‘unsigned char*’
为什么会得到这个错误,我认为一个void是无类型的,所以它可以指向任何东西,或任何东西可以指向它?
Why do I get that error, I thought a void was "type-less" so it can point at anything, or anything can point to it?
推荐答案
您需要强制转换,因为您无法将void *转换为任何未转换的内容。
You need to cast as you can not convert a void* to anything without casting it first.
你需要做
unsigned char* etherhead = (unsigned char*)buffer;
(虽然你也可以使用 static_cast
)
(although you could use a static_cast
also)
要了解有关void指针的更多信息,请查看 6.13 - 无效指针。
To learn more about void pointers, take a look at 6.13 — Void pointers.
这篇关于从“void *”到“unsigned char *”的转换无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文