从"void*"到"void(*)(void*)"的转换无效
Invalid conversion from `void *` to `void (*)(void*)`
所以我目前正在使用,或者至少正在尝试编写一个使用这个C pthread线程池库的程序。
值得注意的是thpool.h
中的以下功能:
int thpool_add_work(threadpool, void (*function_p)(void*), void* arg_p);
我试图添加一个作品的代码如下:
int testpool(string (&input)[3]){
// Pass three strings to it. Which we will end up displaying.
cout << input[0].c_str() << endl;
cout << input[1].c_str() << endl;
cout << input[2].c_str() << endl;
return 1;
}
string input[3];
input[1] = "Hello";
input[2] = "Test";
input[3] = "Happy.";
thpool_add_work(thpool, (void*)testpool, (void*)input);
这给了我以下错误:
main.cpp: In function ‘int main(int, char**)’: main.cpp:167:55: error: invalid conversion from ‘void*’ to ‘void (*)(void*)’ [-fpermissive] thpool_add_work(thpool, (void*)testpool, (void*)input); ^ In file included from main.cpp:29:0: thpool.h:67:5: note: initializing argument 2 of ‘int thpool_add_work(threadpool, void (*)(void*), void*)’ int thpool_add_work(threadpool, void (*function_p)(void*), void* arg_p);
我确信我只是错误地调用了函数,但不知道如何正确地调用它。那么我该如何修复呢?
编辑/更新:
我更改了功能以执行以下操作:
void testpool(void*){
// Pass three strings to it. Which we will end up displaying.
cout << "Hellooooo." << endl;
}
这很好用。现在的问题是,我如何向它传递一个字符串数组,以便可以作为参数访问数据?
void (*function_p)(void*)
意味着您的函数必须具有返回类型void,并将单个void指针作为参数。你的职能并非如此。
thpool_add_work
需要一个指向返回void
并接受单个void*
参数的函数的指针。您的testpool
不是这样一个函数。指向testpool
的指针将具有类型
int (*)(string (&)[3])
这与预期的有很大不同
void (*)(void*)
如果你想在这个库中使用这个函数,你需要稍微改变一下:
void testpool(void* vinput){
string* input = static_cast<string*>(vinput);
// Pass three strings to it. Which we will end up displaying.
cout << input[0].c_str() << endl;
cout << input[1].c_str() << endl;
cout << input[2].c_str() << endl;
}
请注意,我已经更改了参数类型,向string*
添加了强制转换,并删除了return
语句。现在您可以这样调用thpool_add_work
:
thpool_add_work(thpool, testpool, input);
如果你真的需要这个返回值,你需要更进一步,传递一个指向某个结构的指针:
struct TestpoolArgs
{
string input[3];
int ret;
};
void testpool(void* vargs){
TestpoolArgs* args = static_cast<TestpoolArgs*>(vargs);
// Pass three strings to it. Which we will end up displaying.
cout << args->input[0].c_str() << endl;
cout << args->input[1].c_str() << endl;
cout << args->input[2].c_str() << endl;
args->ret = 1;
}
有了这个版本,你的呼叫站点会看起来像这样:
TestpoolArgs args;
args.input[0] = "Hello";
args.input[1] = "Test";
args.input[2] = "Happy.";
thpool_add_work(thpool, testpool, &args);
// Wait until testpool runs somehow
int testpool_return_value = args.ret;
最后一点需要注意的是,在异步调用完成之前保持参数对象的活动状态可能是一个挑战。正如我在这里所做的那样,将它们声明为自动变量意味着在退出声明它们的作用域之前,必须等待异步调用完成,并且不能真正将std::unique_ptr
或std::shared_ptr
与C库一起使用。由于您正在编写C++,因此使用std::async
之类的东西可能会更好。
相关文章:
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 在c++类上调用void函数
- 为什么野牛仍在使用"int yylex(void)",却找不到"int yylex(YYS
- 在派生函数中指定void*参数
- C++为什么尽管我调用了void函数,它却不起作用
- 如何从void函数输出字符串
- 我应该使用什么来代替void作为变体中的替代类型之一
- 奇怪的结构&GCC&clang(void*返回类型)
- Arduino:for/while/if在void setup()或void loop()之前?——错误:之前需要不合格
- 为什么这个函数将"const char*"转换为"void* const"而不是"const void*"
- 引用一个已擦除类型(void*)的指针
- 将尾部调用void(i32,..)位转换为llvm::函数以获取FnAttribute
- 库函数需要一个 std::function<void(void)>,如何传入类函数?
- 如何将指针从一个void函数传递到另一个C++
- 为什么我在使用void函数时得到错误代码C2276
- void*到驱动程序中的UnicodeString
- 为什么在逗号分隔符上下文中将预增量的结果强制转换为void
- 指针没有更新它在void函数内部指向的值
- 不能将 "void *" 类型的值分配给类型 "TCHAR" 的实体
- 错误:在为指针赋值时,void值没有被忽略