错误:'void *'不是一个指针对象类型 [英] Error: ‘void*’ is not a pointer-to-object type

查看:3265
本文介绍了错误:'void *'不是一个指针对象类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从动态库访问函数,实例化一个Person的实例,并返回指向它的指针作为void指针。然后,程序必须使用reinterpret_cast将void指针转换为Person。但是,我收到一个错误:错误:'void *'不是一个指针对象类型。



代码:



函数库:

  void * loadPerson ){
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<库初始化错误。<< endl;
else {
// ...

谢谢!

解决方案

有问题的行是:

  void * person_vp =(* loadPerson)(); 

您将取消引用 void * 。您需要这样:

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

编辑:



为了更好的可读性,演员可以这样拆分:

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


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!

解决方案

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)();

这篇关于错误:'void *'不是一个指针对象类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆