简体   繁体   中英

initializing : cannot convert from LPVOID error

Ok I'm trying to write this under the WFSExecute but if I type:

WFSPINGETDATA * pingetdata = lpCmdData;

I get an error:

errorC2440: 'initializing' : cannot convert from 'LPVOID' to 'WFSPINGETDATA *'

If I comment out that line, the app execute.

Also, if I write:

((WFSPINDATA*) (temp)) ->lpPinKeys = malloc(sizeof(LPWFSPINKEY)*NumberOfKeys) ;

I get an error:

errorC2440: '=' cannot convert from 'void' to 'LPWFSPINKEY *'

Any solution to solve this?

C++ is more strict on type safety than C is. In this case, void* must be type-casted when assigned to anything other than another void* .

 WFSPINGETDATA * pingetdata = lpCmdData; 

cannot convert from 'LPVOID' to 'WFSPINGETDATA *'

This means lpCmdData is a void* , so a type-cast is needed:

WFSPINGETDATA * pingetdata = (WFSPINGETDATA*) lpCmdData;

Or, using a C++-style cast instead of a C-style cast:

WFSPINGETDATA * pingetdata = static_cast<WFSPINGETDATA*>(lpCmdData);
 ((WFSPINDATA*) (temp)) ->lpPinKeys = malloc(sizeof(LPWFSPINKEY)*NumberOfKeys) ; 

cannot convert from 'void' to 'LPWFSPINKEY *'

malloc() returns a void* , so a type-cast is needed here as well:

((WFSPINDATA*) (temp)) ->lpPinKeys = (LPWFSPINKEY*) malloc(sizeof(LPWFSPINKEY)*NumberOfKeys);

Or, using C++-style casts:

static_cast<WFSPINDATA*>(temp)->lpPinKeys = static_cast<LPWFSPINKEY*>(malloc(sizeof(LPWFSPINKEY)*NumberOfKeys));

Or, using C++-style allocation instead of C-style allocation:

static_cast<WFSPINDATA*>(temp)->lpPinKeys = new LPWFSPINKEY[NumberOfKeys];
// must use 'delete[] lpPinKeys' instead of 'free(lpPinKeys)' to deallocate the memory

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM