一个非常着急的问题 谢谢关注

znnren 2003-08-22 10:58:59
我想问的是关于多线程的编程
这是我写的

ThreadID[j]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINEConnect,&i,0,NULL);
这句话是我类中一个成员函数的一句话
其中的Connect也是类的另一成员函数

提示错误是
D:\项目练习\ScanPort\socket.cpp(76) : error C2440: 'type cast' : cannot convert from '' to 'unsigned long (__stdcall *)(void *)'
None of the functions with this name in scope match the target type
Error executing cl.exe.
3x
...全文
29 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
znnren 2003-08-25
  • 打赏
  • 举报
回复
大家说的没错 我看头文件了
BeginScan声明的是static 我疏忽了
小虫说得不错
不过我想我只理解了一点
这片帖子就到这吧
谢谢大家的帮助
farfh 2003-08-25
  • 打赏
  • 举报
回复
ericlin 2003-08-25
  • 打赏
  • 举报
回复
你的 BeginScan(LPVOID* param) 声明里面应该是静态的.
ericlin 2003-08-25
  • 打赏
  • 举报
回复
error C2440: 'type cast' : cannot convert from 'unsigned long (__stdcall Cdmi::*)(void *)' to 'unsigned long (__stdcall *)(void *)'
方法:
(1)'unsigned long (__stdcall Cdmi::*)(void *)'是指向Cdmi某个成员函数的指针.
(2)'unsigned long (__stdcall *)(void *)'仅仅只是一个c形式函数的指针. 编译器无法将(1)转换为(2)是因为c++成员函数取第一个(隐藏)参数"this pointer"作为成员函数,但当是一个静态的成员时则例外.可按如下方法解决.
class XMyThread
{
public:
void StartThread(void);
virtual UINT ThreadFunction(void);
static UINT __bogusthreadfunc(LPVOID lpparam);
};
void XMyThread::StartThread()
{
AfxBeginThread(__bogusthreadfunc,this);
}
UINT XMyThread::ThreadFunction(void)
{
//here you do all your real work
return 0;
}
UINT XMyThread::__bogusthreadfunc(LPVOID lpparam)
{
XMyThread* This = dynamic_cast(lpparam);
return This->ThreadFunction();
}
for the sake of clairty, I did not add StopThread and I did not save the
CWinThread* returned by AfxBeginThread.
If you wanted a thread that does other things, simply derive from XMyThread
and override ThreadFunction()
example:
class XAnotherThread : public XMyThread
{
virtual UINT ThreadFunction(void);
};
UINT XAnotherThread :: ThreadFunction(void)
{
//do some other work here
return 0;
}
thisisyjs 2003-08-23
  • 打赏
  • 举报
回复
是要为静态或全局的,看一下UINT CScanPort:: BeginScan(LPVOID* param)的声明部分我想应该是 static。
思危 2003-08-22
  • 打赏
  • 举报
回复
写错了!

class my
{
public:
UINT connect ( LPVOID pParam );

};
思危 2003-08-22
  • 打赏
  • 举报
回复
class my
{
public:
static connect(void);
};
znnren 2003-08-22
  • 打赏
  • 举报
回复
一定要static吗?
下面是我曾看过的一段代码
CScanPort::CScanPort(char* addr,unsigned start,unsigned end,int threadid)
{
ThreadID = threadid;
if ( addr!=NULL && addr[0]!='\0')
strcpy ( Address, addr);
else
Address[0] = '\0';
c_socket = -1;
sPort = start ;
ePort = end ;
hThread = CreateThread(
NULL, // no security attributes
0, // use default stack size
(LPTHREAD_START_ROUTINE)BeginScan , // thread function
this, // argument to thread function
0, // use default creation flags
NULL); // returns the thread identifier


}

UINT CScanPort:: BeginScan(LPVOID* param)
{
CScanPort* This = (CScanPort*) param;
int i;
fopen ("port.txt","w");

for ( i=This->sPort; i<This->ePort+1 ;i++)
{
This->cPort = i;
if (This-> Open() == 0 )//成功
{
printf ("< %3d > connect port %5d success!\n",This->ThreadID,This-> cPort ) ;
This-> WritetoFile();
}
}
return 0;
}
上面的没有错(能运行) 可是感觉既不是static,也不是全局函数
akun 2003-08-22
  • 打赏
  • 举报
回复
connect一定要是static成员函数,或者全局函数。
XiangDong 2003-08-22
  • 打赏
  • 举报
回复
connect不能是类的非静态成员函数,
XiangDong 2003-08-22
  • 打赏
  • 举报
回复
connect不能是另一个类的非静态成员函数,
bm1408 2003-08-22
  • 打赏
  • 举报
回复
没有区别啊!

这个类不应该是本类本事啊!
FAICHEN 2003-08-22
  • 打赏
  • 举报
回复
connect声明的不对
uint connect(LPVOID pvoid)
bm1408 2003-08-22
  • 打赏
  • 举报
回复
ThreadID[j]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINEConnect,&i,0,NULL);

格式就不对啊!

ThreadID[j]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)Connect,&i,0,NULL);
znnren 2003-08-22
  • 打赏
  • 举报
回复
上面我少打了半个括号

我想问的是当线程函数是类的成语函数时于是一般函数时有区别吗?

调用参数有区别马?
farfh 2003-08-22
  • 打赏
  • 举报
回复
ThreadID[j]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINEConnect,&i,0,NULL);
修改为
ThreadID[j]=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)Connect,&i,0,NULL);

15,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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