gpt4 book ai didi

c++ - 错误 : ‘void*’ is not a pointer-to-object type error while dynamically opening multiple shared libraries

转载 作者:搜寻专家 更新时间:2023-10-31 00:44:20 33 4
gpt4 key购买 nike

我需要在运行时打开多个共享库。我不知道它们的数量(计数),所以我正在使用动态内存分配:

void* handle= new void* [n]; // n refers to number of handles
handle[0] = dlopen("./fileN.so", RTLD_LAZY); // doesn't work : Error: ‘void*’ is not a pointer-to-object type

但是,如果我进行静态分配,它会起作用-

void* handle[10];
handle[0] = dlopen("./file0.so", RTLD_LAZY); // works

为什么当我动态访问句柄时出现错误?我该如何解决?

最佳答案

您需要额外的间接级别。指向指针的指针:

void** handle= new void* [n];
^

您的代码在其他类型上将无效:

int* handle= new int* [n]; // error assigning int** to int*

但它适用于 void*,因为 void* 可以指向 void**

关于c++ - 错误 : ‘void*’ is not a pointer-to-object type error while dynamically opening multiple shared libraries,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8959406/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com