Reputation:
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!
Upvotes: 2
Views: 5357
Reputation: 171167
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)();
Upvotes: 2