求教,C++类中创建线程函数的问题

sunnysab 2013-10-25 08:15:05
麻烦各位高手了,还望赐教。
有一个类,两个函数。一个函数是线程函数:


#include <windows.h>
#include <stdio.h>

class Class
{
public:

int i;

DWORD WINAPI ThreadProc( LPVOID lpParameter );
int StartThread();

};

DWORD WINAPI Class::ThreadProc( LPVOID lpParameter )
{
int j = (int)lpParameter;

printf("%d + %d = %d\n", i, j, i + j);

return 0;
}

int Class::StartThread()
{
CreateThread(NULL, 0, ThreadProc, (LPVOID)123, 0, 0);
}

int main()
{
Class c1;

c1.StartThread();

return 0;
}

编译后提示:
--------------------Configuration: Cpp1 - Win32 Debug--------------------
Compiling...
Cpp1.cpp
D:\System\桌面\Cpp1.cpp(27) : error C2664: 'CreateThread' : cannot convert parameter 3 from 'unsigned long (void *)' to 'unsigned long (__stdcall *)(void *)'
None of the functions with this name in scope match the target type
执行 cl.exe 时出错.

Cpp1.obj - 1 error(s), 0 warning(s)


然后,按照网上说的,在线程函数前加static,出现如下提示:
--------------------Configuration: Cpp1 - Win32 Debug--------------------
Compiling...
Cpp1.cpp
D:\System\桌面\Cpp1.cpp(20) : error C2597: illegal reference to data member 'Class::i' in a static member function
D:\System\桌面\Cpp1.cpp(20) : error C2597: illegal reference to data member 'Class::i' in a static member function
执行 cl.exe 时出错.

Cpp1.obj - 1 error(s), 0 warning(s)

接下来怎么办……不想把 i 也设置成static。
不知道这个问题应该发到线程板块还是类板块,于是就这么发了,如果管理员觉得不妥,可以移到基础类板块
...全文
304 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
sunnysab 2013-10-26
  • 打赏
  • 举报
回复
总算可以了,谢谢大家。 另外,是在类外加int Class::i;,没有static。
worldy 2013-10-25
  • 打赏
  • 举报
回复
在类外面加上 static int Class::i;
真相重于对错 2013-10-25
  • 打赏
  • 举报
回复
#include <windows.h> #include <stdio.h> class Class { public: static int i; static DWORD WINAPI ThreadProc( LPVOID lpParameter ); int StartThread(); }; DWORD WINAPI Class::ThreadProc( LPVOID lpParameter ) { int j = (int)lpParameter; // i = 2; printf("%d\n", i); return 0; } int Class::StartThread() { CreateThread(NULL, 0, ThreadProc, (LPVOID)2, 0, 0); Sleep(1000); return 0; } int Class::i; int main() { Class c1; c1.StartThread(); return 0; }
sunnysab 2013-10-25
  • 打赏
  • 举报
回复
对了,一直忘了说明,VC6 + WinXP下
sunnysab 2013-10-25
  • 打赏
  • 举报
回复
引用 5 楼 worldy 的回复:
要明确的传递this指针
刚才又在网上看,静态函数不接受隐含的this。说把所有成员改成static的 于是我又改啊改,改成这样:

#include <windows.h>
#include <stdio.h>

class  Class
{
public:

    static int  i;

    static DWORD WINAPI ThreadProc( LPVOID lpParameter );
    int  StartThread();

};

DWORD WINAPI Class::ThreadProc( LPVOID lpParameter )
{
    int  j = (int)lpParameter;

 //   i = 2;
    printf("%d\n", i);

    return 0;
}

int  Class::StartThread()
{
    CreateThread(NULL, 0, ThreadProc, (LPVOID)2, 0, 0);
    Sleep(1000);
    return 0;
}

int main()
{
    Class  c1;

    c1.StartThread();

    return 0;
}
link的时候,又出错了……
--------------------Configuration: Cpp1 - Win32 Debug--------------------
Linking...
Cpp1.obj : error LNK2001: unresolved external symbol "public: static int Class::i" (?i@Class@@2HA)
Debug/Cpp1.exe : fatal error LNK1120: 1 unresolved externals
执行 link.exe 时出错.

Cpp1.exe - 1 error(s), 0 warning(s)
接下来如何是好。。。
worldy 2013-10-25
  • 打赏
  • 举报
回复
要明确的传递this指针
sunnysab 2013-10-25
  • 打赏
  • 举报
回复
引用 3 楼 worldy 的回复:
也就是: static DWORD WINAPI Class::ThreadProc( LPVOID lpParameter ) { int j = (int)lpParameter; printf("%d + %d = %d\n", i, j, i + j); return 0; }
请问static成员函数如何访问类成员变量呢?
worldy 2013-10-25
  • 打赏
  • 举报
回复
也就是: static DWORD WINAPI Class::ThreadProc( LPVOID lpParameter ) { int j = (int)lpParameter; printf("%d + %d = %d\n", i, j, i + j); return 0; }
worldy 2013-10-25
  • 打赏
  • 举报
回复
线程必须是类的静态函数,否则,肯定不行,因为附加的,类非静态函数必须传递this指针
sunnysab 2013-10-25
  • 打赏
  • 举报
回复
顺便补充下,也不想把函数移到类外做全局函数。 网上还有人说把 this 传到线程函数参数,但是我现在需要自己也传参给线程

15,471

社区成员

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

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