[英]invalid conversion from ‘void*’ to ‘void* (*)(void*)’ c++?
I am trying to use pthread_create() but it always gives me this error invalid conversion from void*
to void* ( * )(void*)
我试图使用pthread_create()但它总是给我这个错误无效转换从
void*
到void* ( * )(void*)
This error is in the 3rd argument. 此错误在第3个参数中。 Could someone help me with this error ?
有人可以帮我解决这个错误吗?
void Print_data(void *ptr) {
cout<<"Time of Week = " <<std::dec<<iTOW<<" seconds"<<endl;
cout<<"Longitude = "<<lon<<" degree"<<endl;
cout<<"Latitude = "<<lat<<" degree"<<endl;
cout<<"Height Above Sea = "<<alt_MSL<<" meters"<<endl;
}
int call_thread()
{
pthread_create(&thread, NULL, (void *) &Print_data, NULL);
return 0;
}
The error is that you're converting the function pointer ( void* (*)(void*)
) to an object pointer ( void*
), when pthread_create
expects a function pointer for that argument. 当
pthread_create
需要该参数的函数指针时,错误是您将函数指针( void* (*)(void*)
)转换为对象指针( void*
)。 There's no implicit conversion to undo the dodgy conversion you've done, hence the error. 没有隐式转换来撤消你已经完成的狡猾的转换,因此错误。
The answer is to not do that: 答案是不这样做:
pthread_create(&thread, NULL, &Print_data, NULL);
and you'll also need to modify Print_data
to return void*
to match the Posix threads interface: 并且您还需要修改
Print_data
以返回void*
以匹配Posix线程接口:
void *Print_data(void *ptr) {
// print stuff
return NULL; // or some other return value if appropriate
}
As noted in the comments, there are various other issues with using this C library directly from C++; 正如评论中所指出的,直接从C ++使用这个C库还有其他各种问题。 in particular, for portability, the thread entry function should be
extern "C"
. 特别是,为了便于携带,线程入口函数应该是
extern "C"
。 Personally, I'd recommend using the standard C++ thread library (or Boost's implementation, if you're stuck with a pre-2011 version of the language). 就个人而言,我建议使用标准的C ++线程库(或Boost的实现,如果你坚持使用2011年之前的语言版本)。
You're trying to convert a function pointer into a void*
here: (void *) &Print_data
您正在尝试将函数指针转换为
void*
here: (void *) &Print_data
According to pthread_create
you need to pass in a function that takes a void*
and returns a void*
根据
pthread_create
你需要传入一个带有void*
的函数并返回一个void*
So your function signature should be 所以你的功能签名应该是
void* Print_data(void *ptr)
And your call should be 你的电话应该是
pthread_create(&thread, NULL, &Print_data, NULL);
pthread_create
takes the third argument as pthread_create
将第三个参数作为
int pthread_create(pthread_t *thread,
const pthread_attr_t *attr,
void *(*start_routine)(void*),
void *arg);
This, void *(*start_routine)(void*)
is a pointer to a function that takes a void*
pointer and returns a void*
pointer. 这,
void *(*start_routine)(void*)
是一个指向函数的指针,该函数接受void*
指针并返回void*
指针。
When you do &Print_data
and convert the pointer to void *
, it means you are passing a pointer of type void*
and not a pointer of type void *(*start_routine)(void*)
[function pointer]. 当您执行
&Print_data
并将指针转换为void *
,这意味着您传递的是void*
类型的指针,而不是类型为void *(*start_routine)(void*)
[函数指针]的指针。
To be correct, you need to make your return type as void*
and make the call as pthread_create(&thread, NULL, &Print_data, NULL);
要正确,您需要将返回类型设置为
void*
并将调用作为pthread_create(&thread, NULL, &Print_data, NULL);
You must return void*
你必须返回
void*
void* Print_data(void *ptr) {
to satisfy the needs. 满足需求。
The signature of the function to be passed is 要传递的函数的签名是
void* function(void*);
then call pthread_create
using 然后使用调用
pthread_create
pthread_create(&thread, NULL, &Print_data, NULL);
添加头文件#include并编译g ++ -lpthread
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.