简体   繁体   English

从`void *`到`void(*)(void *)`的无效转换

[英]Invalid conversion from `void *` to `void (*)(void*)`

So I'm currently using, or at least trying to write a program that makes use of this C pthread threadpool library. 因此,我目前正在使用或至少试图编写一个使用此C pthread线程池库的程序。

Of note is the following function in thpool.h : 值得注意的是thpool.h的以下功能:

int thpool_add_work(threadpool, void (*function_p)(void*), void* arg_p);

My code which I'm trying to add a work to is as follows: 我要添加工作的代码如下:

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

This gives me the following error: 这给了我以下错误:

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

I'm certain that I'm simply calling the function wrong or something, but can't figure out how exactly to do it properly. 我敢肯定,我只是在调用该函数错误之类的东西,但无法弄清楚如何正确地正确执行该函数。 So how do I fix it? 那么我该如何解决呢?

Edit/Update: 编辑/更新:

I changed the function to do the following: 我将功能更改为执行以下操作:

void testpool(void*){
    // Pass three strings to it. Which we will end up displaying.
    cout << "Hellooooo." << endl;
}

And this works fine. 这很好。 Now question is how do I pass this an array of strings so I can access the data as arguments? 现在的问题是,如何传递此字符串数组,以便可以将数据作为参数访问?

void (*function_p)(void*)

means that your function MUST have the return type void and takes a single void pointer as argument. 意味着您的函数必须具有返回类型void并接受单个void指针作为参数。 This is not the case for your function. 您的功能并非如此。

thpool_add_work expects a pointer to a function that returns void and takes a single void* argument. thpool_add_work需要一个指向返回void并接受单个void*参数的函数的指针。 Your testpool is not such a function. 您的testpool不是这样的功能。 A pointer to testpool would have the type 指向testpool的指针的类型为

int (*)(string (&)[3])

which is quite a bit different than the expected 这与预期的有很大的不同

void (*)(void*)

If you want to use that function with that library, you will need to change it slightly: 如果要将该函数与该库一起使用,则需要对其稍作更改:

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;
}

Note that I've changed the argument type, added a cast to string* , and removed the return statement. 请注意,我已经更改了参数类型,对string*添加了强制类型转换,并删除了return语句。 Now you can call thpool_add_work like this: 现在,您可以像这样调用thpool_add_work

thpool_add_work(thpool, testpool, input);

If you really need that return value, you'll need to go one step further and pass a pointer to some struct: 如果您确实需要该返回值,则需要进一步走一步并将一个指针传递给某个struct:

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;
}

With this version, your call site would look something like this: 使用此版本,您的呼叫站点将如下所示:

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;

One last note is that keeping your argument objects alive until an asynchronous call completes could be a challenge. 最后一点是,在异步调用完成之前保持参数对象处于活动状态可能是一个挑战。 Declaring them as automatic variables as I've done here means that you have to wait for the asynchronous call to complete before you exit the scope in which they're declared, and you can't really use std::unique_ptr or std::shared_ptr with a C library. 正如我在这里所做的那样,将它们声明为自动变量意味着您必须等待异步调用完成才能退出声明它们的范围,并且您实际上不能使用std::unique_ptrstd::shared_ptr带C库的std::shared_ptr You're likely better off using something like std::async since you're writing C++. 由于正在编写C ++,因此最好使用std::async类的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 'void* (*)(int*)' 到 'void* (*)(void*)' 的无效转换 - invalid conversion from 'void* (*)(int*)' to 'void* (*)(void*)' 从“void*”到“void (*)(..)”的无效转换 - Invalid conversion from 'void*' to 'void (*)(..)' 无效转换从void *到void(*)(void *)[ - fpermissive] - invalid conversion from void* to void(*)(void*)[-fpermissive] 错误:从'void(*)()'到'void(*)()'的转换无效 - 什么? - error: invalid conversion from 'void (*)()' to 'void (*)()' — what? 从&#39;DWORD(*)(void *)&#39;到&#39;DWORD(*)(void *)&#39;的无效转换 - invalid conversion from 'DWORD (*)(void*)' to 'DWORD (*)(void*)' 在OpenCL clCreateContext中从void(*)(...)到void(*)(...)的转换无效 - Invalid conversion from void (*) (…) to void (*) (…) in OpenCL clCreateContext 从&#39;void *&#39;到&#39;void *(*)(void *)&#39;c ++的无效转换? - invalid conversion from ‘void*’ to ‘void* (*)(void*)’ c++? 错误:从'void *'到'void *(*)(void *)'的无效转换 - pthreads - error: invalid conversion from ‘void*’ to ‘void* (*)(void*)’ - pthreads 错误:从&#39;int(*)(void *)&#39;到&#39;void *(*)(void *)&#39;的转换无效 - error: invalid conversion from ‘int (*)(void*)’ to ‘void* (*)(void*)’ 从void *到int *的无效转换 - Invalid conversion from void* to int*
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM