多线程编程,编译时提示invalid use of member (did you forget the &),找不到原因。

phz1985 2009-01-01 09:22:59

#include "igrs.h"

//Singlecast Message Server.
void CDevice::SingleMsgServer(string strHost, unsigned uPort)
{
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
inet_aton(strHost.c_str(), &addr.sin_addr);
addr.sin_port = htons(uPort);
int s = socket(AF_INET, SOCK_STREAM, 0);
if (s < 0)
{
perror("socket error in SingleMsgServer\n");
return ;
}
if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0)
{
perror("bind error in SingleMsgServer\n");
return ;
}
if (listen(s, LISTEN_MAX_NUM) < 0)
{
perror("listen error in SingleMsgServer\n");
return ;
}
struct sockaddr_in clientAddr;
socklen_t len;
pthread_t th;
for (;;)
{
int newS = accept(s, (struct sockaddr *)&clientAddr, &len);
if (newS < 0)
{
perror("accept error in SingleMsgServer.\n");
}
//create a thread which runs handleSingleMsg to handle the new connect.
//错误在这一行
if (pthread_create(&th, NULL, (void *(*)(void *))HandleSingleMsg, (void *)&newS) < 0)
{
perror("pthread_create error in SingleMsgServer.\n");
}
}
}

//When SingleMsgServer accepts a connect request,
//HandleSingleMsg handles the connect as a thread.
void CDevice::HandleSingleMsg(int fd)
{
int n;
char tmp[BUF_MAX_SIZE];
for (;;)
{
if ((n = recv(fd, tmp, BUF_MAX_SIZE, 0)) < 0)
{
perror("recv error in SingleMsgServer.\n");
}
else
{
tmp[n] = '\0';
string ackMsg = MsgResolver(tmp);
//if gotten message is request message, then send response message.
if (!ackMsg.empty())
{
if (send(fd, ackMsg.c_str(), ackMsg.size(), 0) < 0)
{
perror("send error in SingleMsgServer.\n");
}
}
}
}
}




# g++ -c single_msg_server.cpp
single_msg_server.cpp: In member function ‘void CDevice::SingleMsgServer(std::string, unsigned int)’:
single_msg_server.cpp:38: 错误:invalid use of member (did you forget the ‘&’ ?)
使用的编译是g++.
...全文
1374 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
dsadsadsa11 2009-01-09
  • 打赏
  • 举报
回复
。。。1 ,3 楼的两位,我看你们没有相关经验
joyself 2009-01-09
  • 打赏
  • 举报
回复
支持楼上两位


请贴出头文件,

还有在语句
if (pthread_create(&th, NULL, (void *(*)(void *))HandleSingleMsg, (void *)&newS) < 0)
里的转换 (void *(*)(void *))HandleSingleMsg 确实让人眼花缭乱。


个人觉得如果你的HandleSingleMsg 函数定义的返回类型不是void *,那么你做个转换试试:
(void *) & HandleSingleMsg

dsadsadsa11 2009-01-09
  • 打赏
  • 举报
回复
楼主,你不贴头文件我只有猜想了,我猜你的HandleSingleMsg不是静态成员函数,就是没有加static标识符。
类非静态的成员函数是不能作为线程函数启动的,因为this指针的缘故。我建议楼主检查下头文件,看看是否是忘了给HandleSingleMsg前面加上static标识。
qsxiaoyao 2009-01-09
  • 打赏
  • 举报
回复
mark
dsadsadsa11 2009-01-09
  • 打赏
  • 举报
回复
个人觉得如果你的HandleSingleMsg 函数定义的返回类型不是void *,那么你做个转换试试:
(void *) & HandleSingleMsg

HandleSingleMsg这些函数名本来就是指针的
leeyiqun 2009-01-01
  • 打赏
  • 举报
回复
if (pthread_create(&th, NULL, (void *(*)(void *))HandleSingleMsg, (void *)&newS) < 0)
改成这样:
if (pthread_create(&th, NULL, HandleSingleMsg, (void *)&newS) < 0)

然后 void CDevice::HandleSingleMsg(int fd)
改成 void *CDevice::HandleSingleMsg(int fd)

23,125

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧