我正在运行与在纯C中运行的完全相同的代码:
pthread_create(&threads[i], &attr, SomeMethod, ptrChar);
Run Code Online (Sandbox Code Playgroud)
我得到错误:
错误:从'void*(
*)(char'*)' to 'void*(*)(void*)' 转换无效错误:初始化'int的参数3
pthread_create(__pthread_t**, __pthread_attr_t* conts*, void*(*)(void*), void*)'
SomeMethod是:
void *SomeMethod(char *direction)
Run Code Online (Sandbox Code Playgroud)
我需要在C++中做一些不同的事情吗?我以为你可以在C++中运行任何C代码,它会工作正常吗?
我现在正在使用Cygwin.
就像它说的那样,它们是两个不同的功能签名.你应该做:
void *SomeMethod(void* direction) // note, void
{
char* dir = static_cast<char*>(direction); // and get the value
}
Run Code Online (Sandbox Code Playgroud)
显然,C从一个函数指针转换到另一个.将一个函数指针转换为另一个函数指针是C++中未定义的行为.
也就是说,我非常确定POSIX要求函数指针之间的转换是明确定义的,所以你可以这样做:
pthread_create(&threads[i], &attr, // cast the function pointer
reinterpret_cast<void* (*)(void*)>(SomeMethod), ptrChar);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
568 次 |
| 最近记录: |