在class Foo
我有两种方法,assign_handler()
和call_handler()
。从另一种方法调用成员方法指针
实际处理程序代码位于main.cpp
,即do_this()
。 do_this()
使用在main.cpp
的一些全局变量,
我觉得富都必须有一个函数指针将在assign_handler()
这是我做过什么来分配的成员。不过,我在调用assign_handler()
时遇到问题,即从call_handler()
调用do_this()
。
注意:call_handler()
本身是由Foo
中的sigaction
调用的。
编辑:我试着按照评论中的建议产生一个MCVE。我用gedit来创建文件并在命令行中使用g ++进行编译。代码起作用。但是在我的Eclipse项目中,我收到了代码的内联注释中显示的错误。
MCVE:
//Foo.h
class Foo{
public:
void (*funptr)(void);
void call_handler();
void assign_handler (void(*func1)(void));
Foo(){};
};
//Foo.cpp
#include "Foo.h"
void Foo::assign_handler(void(*func1)(void)){
funptr = func1;
}
void Foo::call_handler(){
funptr();//error: invalid use of member Foo::funptr in static member function; from this location
//or
//this->funptr();//error: 'this' is unavailable for static member functions
}
//main.cpp
#include <iostream>
#include "Foo.h"
using namespace std;
void do_this(void);
int main(void){
Foo foo;
foo.assign_handler(do_this);
foo.call_handler(); //this won't be called explicitly, it is assigned as a handler for a sigaction
int x;
cin>>x;
}
void do_this(void){
cout<<"done"<<endl;
}
发布[最小,完整和可验证示例](http:// stackoverflow。com/help/mcve)你的代码 – Thomas
我假设'call_handler'是一个静态成员函数,而不是你的例子中的常规成员? – villintehaspam
@villintehaspam我刚刚意识到我在类定义中留下了static关键字。删除关键字可消除错误。我会先忽略静态关键字在代码中的重要性,然后再忽略它。谢谢。 –