我为一个变量分配了一个函数
p_samplen_->onData = onData;
PVOID onData(PVOID args) {
}
然后就成功了。
我试着把这个变量赋给类中的一个函数,但没有成功。
p_samplen_->onData = &MyClass::onData;
PVOID MyClass::onData(PVOID args) {
}
错误是
test.cpp:32:48: error: assigning to 'startRoutine' (aka 'void *(*)(void *)') from incompatible type 'void *(MyClass::*)(void *)'
请帮我解决这个问题。
你的语法可能不正确。成员指针与普通指针的类型类别不同。成员指针必须与其类的对象一起使用。
#include <iostream>
class MyClass
{
public:
void *f(void *);
void *(MyClass::*x)(void *); // <- declare by saying what class it is a pointer to
};
void *MyClass::f(void *ptr)
{
return ptr;
}
int main()
{
MyClass a;
a.x = &MyClass::f; // use the :: syntax
int b = 100;
std::cout << *(int *)((a.*(a.x)) (&b)) << std::endl;
return 0;
}
OR
例如:
#include <iostream>
#include <functional>
class MyClass
{
public:
void *f(void *);
std::function<void *(void *)> function;
};
void *MyClass::f(void *ptr)
{
return ptr;
}
int main()
{
int b = 100;
MyClass a;
a.function = std::bind(&MyClass::f, &a, &b);
std::cout << *(int *)a.function(&b) << std::endl;
return 0;
}