可能重复:
类中的 pthread 函数
我对 c++ 相当陌生,我正在做一个关于 TCP 的项目。
我需要创建一个线程,所以我用谷歌搜索并找到了这个。 http://www.yolinux.com/TUTORIALS/LinuxTutorialPosixThreads.html
我遵循它的语法但遇到错误:'void* (ns3::TcpSocketBase::)()' 类型的参数与'void* ( )(void )'不匹配</p>
代码:
tcp-socket-base.h:
class TcpSocketBase : public TcpSocket
{
public:
...
void *threadfunction();
....
}
tcp-socket-base.cc:
void
*TcpSocketBase::threadfunction()
{
//do something
}
..//the thread was create and the function is called here
pthread_t t1;
int temp = pthread_create(&t1, NULL, ReceivedSpecialAck, NULL); //The error happens here
return;
...
任何帮助,将不胜感激。谢谢!
编辑:
我接受了建议并使线程函数成为非成员函数。
namespaceXXX{
void *threadfunction()
int result = pthread_create(&t1, NULL, threadfunction, NULL);
NS_LOG_LOGIC ("TcpSocketBase " << this << " Create Thread returned result: " << result );
void *threadfunction()
{
.....
}
}
但我得到了这个错误:
初始化 'int pthread_create(pthread_t*, const pthread_attr_t*, void* ( )(void ), void*)' [-fpermissive] 的参数 3