有一个类,两个函数。一个函数是线程函数:
#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。
不知道这个问题应该发到线程板块还是类板块,于是就这么发了,如果管理员觉得不妥,可以移到基础类板块
10 个解决方案
#1
顺便补充下,也不想把函数移到类外做全局函数。
网上还有人说把 this 传到线程函数参数,但是我现在需要自己也传参给线程
网上还有人说把 this 传到线程函数参数,但是我现在需要自己也传参给线程
#2
线程必须是类的静态函数,否则,肯定不行,因为附加的,类非静态函数必须传递this指针
#3
也就是:
static DWORD WINAPI Class::ThreadProc( LPVOID lpParameter ) { int j = (int)lpParameter; printf("%d + %d = %d\n", i, j, i + j); return 0; }
static DWORD WINAPI Class::ThreadProc( LPVOID lpParameter ) { int j = (int)lpParameter; printf("%d + %d = %d\n", i, j, i + j); return 0; }
#4
请问static成员函数如何访问类成员变量呢?
#5
要明确的传递this指针
#6
刚才又在网上看,静态函数不接受隐含的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)
接下来如何是好。。。
#7
对了,一直忘了说明,VC6 + WinXP下
#8
#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;
}
#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;
}
#9
在类外面加上
static int Class::i;
static int Class::i;
#10
总算可以了,谢谢大家。
另外,是在类外加int Class::i;,没有static。
另外,是在类外加int Class::i;,没有static。
#1
顺便补充下,也不想把函数移到类外做全局函数。
网上还有人说把 this 传到线程函数参数,但是我现在需要自己也传参给线程
网上还有人说把 this 传到线程函数参数,但是我现在需要自己也传参给线程
#2
线程必须是类的静态函数,否则,肯定不行,因为附加的,类非静态函数必须传递this指针
#3
也就是:
static DWORD WINAPI Class::ThreadProc( LPVOID lpParameter ) { int j = (int)lpParameter; printf("%d + %d = %d\n", i, j, i + j); return 0; }
static DWORD WINAPI Class::ThreadProc( LPVOID lpParameter ) { int j = (int)lpParameter; printf("%d + %d = %d\n", i, j, i + j); return 0; }
#4
请问static成员函数如何访问类成员变量呢?
#5
要明确的传递this指针
#6
刚才又在网上看,静态函数不接受隐含的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)
接下来如何是好。。。
#7
对了,一直忘了说明,VC6 + WinXP下
#8
#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;
}
#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;
}
#9
在类外面加上
static int Class::i;
static int Class::i;
#10
总算可以了,谢谢大家。
另外,是在类外加int Class::i;,没有static。
另外,是在类外加int Class::i;,没有static。