c ++错误:在静态成员函数中无效使用成员“calls_object :: OBJECT” [英] c++ error: invalid use of member 'calls_object::OBJECT' in static member function
问题描述
我从下面的代码中得到了以下错误。
错误:无效的成员函数'calls_object :: OBJECT' |
错误:来自此位置
c $ c> OBJECT-> call(); 第29行。
基本上,函数必须是静态的,因为它真的是简化版本代码
创建一个windows线程。我只是不能在静态函数中使用指针,但我可以创建静态函数中的对象没有问题。是否有其他方法?
CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)windowsthread,(LPVOID)i,NULL,&m_id );
static DWORD_PTR WINAPI windowsthread()
{
OBJECT-> call();
}
l
类对象
{
private:
public:
object(){}
〜 object(){}
void call(){}
};
class call_object
{
private:
object * OBJECT;
public:
call_object()
{
OBJECT = new object();
}
〜calls_object(){}
#ifdef _WIN32
static void windows_function()
{
OBJECT-& );
}
#endif
};
int main()
{
calls_object O;
}
:
static void windows_function()
{
OBJECT-> call
}
声明为 static
。这意味着它不会收到一个隐式的这个
指针:换句话说,它不会在 call_object的实例
。因此,它不能看到 OBJECT
成员变量。
将函数声明为非静态, code>作为 static
成员变量(在应用程序中更有意义)。
基本上,函数必须是静态的,因为它真的是创建一个windows线程的代码的简化版本
因为你(不幸的是)处理一个接受函数指针的函数( CreateThread
),你甚至不能使用 std: :bind
。然而, CreateThread
允许你提供一个接受一个指针的函数( void
,见原型 ThreadProc
)。
只需传递指向对象
的指针作为 CreateThread
,并让 windowsFunction(void *)
接收该指针。在内部 windowsFunction()
,它仍然是 static
或全局(实际上,你不需要 call_object
class),你可以将该指针转换为指向 object
的指针,并调用 call c。
/ code>泄漏内存,因为您忘记了
delete
在 calls_object
中创建的指针构造函数: 〜calls_object()
{
delete object; //不要忘记!
}
I got the following error from the code below.
error: invalid use of member 'calls_object::OBJECT' in static member function|
error: from this location
from the line OBJECT->call();
line 29.
Basically the function must be static because its really a simplified version of the code which creates a windows thread. I just can't seem to use pointers within a static function but I can create the object within the static function no problem. Is there another way?
CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)windowsthread, (LPVOID)i, NULL, &m_id);
static DWORD_PTR WINAPI windowsthread()
{
OBJECT->call();
}
l
class object
{
private:
public:
object(){}
~object(){}
void call(){}
};
class calls_object
{
private:
object* OBJECT;
public:
calls_object()
{
OBJECT = new object();
}
~calls_object(){}
#ifdef _WIN32
static void windows_function()
{
OBJECT->call();
}
#endif
};
int main()
{
calls_object O;
}
This function:
static void windows_function()
{
OBJECT->call();
}
Is declared as static
. It means it does not receive an implicit this
pointers: in other words, it does not operate on an instance of calls_object
. Therefore, it cannot see the OBJECT
member variable.
Either declare the function as non-static, or declare OBJECT
as a static
member variable (whatever makes more sense in your application).
Basically the function must be static because its really a simplified version of the code which creates a windows thread
Since you are (unfortunately) dealing with a function (CreateThread
) that accepts a function pointer, you cannot even use std::bind
. However, CreateThread
lets you provide a function which accepts a pointer (to void
, see the prototype of ThreadProc
).
Just pass a pointer to an object
as the fourth argument to CreateThread
, and let windowsFunction(void*)
receive that pointer. Inside windowsFunction()
, which would still be static
or global (in fact, you do not need the calls_object
class at all), you can cast that pointer to a pointer to object
and invoke call()
on it.
Also notice, that your class calls_object
is leaking memory, since you are forgetting to delete
the pointer you created in calls_object
's constructor:
~calls_object()
{
delete object; // DON'T FORGET THIS!
}
这篇关于c ++错误:在静态成员函数中无效使用成员“calls_object :: OBJECT”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!