本文介绍了在MSVC中对void *执行指针算术时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Error 1 error C2036: 'const void *' : unknown size file.cpp 111
我不跟着。 GCC从不抱怨void *指针运算,即使在 -ansi -pedantic -Wall
。有什么问题?
I don't follow. GCC never complains about void * pointer arithmetic, even on -ansi -pedantic -Wall
. What's the problem?
以下是代码 -
struct MyStruct {
const void *buf; // Pointer to buffer
const void *bufpos; // Pointer to current position in buffer
};
...
size_t someSize_t, anotherSize_t;
MyStruct *myStruct = (MyStruct *) userdata;
...
if ( (myStruct->bufpos + someSize_t) >
(myStruct->buf + anotherSize_t) ) { // Error on this line
...
推荐答案
void *
指针。对于编译器知道如何处理的事物,转换 oData-> bufpos
和 oData-> anotherConstVoidPtr
。因为你似乎在寻找尺寸,大概是字节,转换到 char *
应该工作:
You can't do pointer math on a void *
pointer. Cast oData->bufpos
and oData->anotherConstVoidPtr
to something the compiler knows how to deal with. Since you seem to be looking for sizes, which are presumably in bytes, casting to char *
should work:
if (((char *)oData->bufpos + someSize_t) ...
这篇关于在MSVC中对void *执行指针算术时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!