sách gpt4 ai đã đi

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

In lại 作者:行者123 更新时间:2023-11-27 23:56:45 28 4
mua khóa gpt4 Nike

所以我目前正在使用,或者至少正在尝试编写一个利用 this C pthread threadpool library. 的程序

值得注意的是 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;
}

这很好用。现在的问题是我如何传递一个字符串数组以便我可以将数据作为参数访问?

1 Câu trả lời

void (*function_p)(void*)

表示您的函数必须具有返回类型 void 并采用单个 void 指针作为参数。您的函数不是这种情况。

关于c++ - 从 `void *` 到 `void (*)(void*)` 的无效转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42180039/

28 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com