Error: ‘void*’ is not a pointer-to-object type

只愿长相守 提交于 2019-12-24 02:56:45

问题


I'm trying to access function from dynamic library, that instantiates an instance of Person, and returns pointer to it as void pointer. The program then has to cast the void pointer to a Person, using a reinterpret_cast. But, I'm getting an error: error: ‘void*’ is not a pointer-to-object type.

Here is the code:

function from library:

void* loadPerson (void) {
    return reinterpret_cast<void*>(new Person);
}

main.cpp:

void* loadPerson = dlsym(lib_handle, "loadPerson");
void* person_vp = (*loadPerson)();
Person* person = reinterpret_cast<Person*>(person_vp);

if (dlerror() != NULL) 
   cout<<"Library init error."<<endl;  
else {
   //...

Thanks!


回答1:


The problematic line is:

void* person_vp = (*loadPerson)();

You're dereferencing a void*. You need this:

void* person_vp = (*reinterpret_cast<void* (*)()>(loadPerson))();

EDIT:

For better readability, the cast can be split like this:

typedef void* VoidFunc();
VoidFunc* loadPerson_func = reinterpret_cast<VoidFunc*>(loadPerson);
void* person_vp = (*loadPerson_func)();


来源:https://stackoverflow.com/questions/13744617/error-void-is-not-a-pointer-to-object-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!