Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/159.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 正在设置类构造函数中的函数指针'&';:对绑定成员函数表达式的非法操作_C++_Compiler Errors_Function Pointers - Fatal编程技术网

C++ 正在设置类构造函数中的函数指针'&';:对绑定成员函数表达式的非法操作

C++ 正在设置类构造函数中的函数指针'&';:对绑定成员函数表达式的非法操作,c++,compiler-errors,function-pointers,C++,Compiler Errors,Function Pointers,我试图在类的构造函数上设置函数指针 我收到以下错误: 错误1错误C2276:“&”:对绑定成员函数表达式的操作非法 谁能告诉我为什么会发生这种情况(代码如下) 编辑: 这个问题的例子更好地说明了我的情况。我只能改变我的班级。我不能触摸我的基地或它的内容 class MyBase { public: void (*MyFunctionPointer)(int args); }; class MyClass : public MyBase { public: void My

我试图在类的构造函数上设置函数指针

我收到以下错误:

错误1错误C2276:“&”:对绑定成员函数表达式的操作非法

谁能告诉我为什么会发生这种情况(代码如下)

编辑:

这个问题的例子更好地说明了我的情况。我只能改变我的班级。我不能触摸我的基地或它的内容

class MyBase
{
public:

    void (*MyFunctionPointer)(int args);

};

class MyClass : public MyBase
{
public:

    void MyFunction(int args)
    {
    }

    MyClass()
    {
        MyFunctionPointer = &MyFunction;
    }
};

int _tmain(int argc, _TCHAR* argv[])
{
    MyClass temp;

    return 0;
}
class MyBase
{
public:

    void (*MyFunctionPointer)(int args);

};

class MyClass : public MyBase
{
public:

    void MyFunction(int args)
    {
    }

    void Bind(void (*myFunctionPointer)(int args))
    {
        MyFunctionPointer = myFunctionPointer;
    }
};

void MyFunction2(int args)
{
}

int _tmain(int argc, _TCHAR* argv[])
{
    MyBase temp1;
    temp1.MyFunctionPointer = &MyFunction2;        // <<-- ok
    temp1.MyFunctionPointer(0);

    MyClass temp2;
    temp2.Bind(&temp2.MyFunction);  // <<-- compile error
    temp2.MyFunctionPointer(0);

    return 0;
}

MyFunction
是一个成员函数,但
MyFunctionPointer
是一个基本函数指针


您应该研究生成的语法。

指向方法函数的指针与指向全局函数和静态方法的指针的类型不同

class MyClass;

class MyBase
{
public:

    void (MyClass::* MyFunctionPointer)(int);

};

class MyClass : public MyBase
{
public:

    void MyFunction(int args)
    {
    }

    MyClass()
    {
        MyFunctionPointer = &MyClass::MyFunction;
    }
};

int main(int argc, char* argv[])
{
    MyClass temp;

    return 0;
}

如果无法更改
MyBase
,则不能使用成员
MyFunctionPointer
,因为它属于指向全局函数或静态方法的指针类型。如果你告诉我们你到底想达到什么,也许我们可以帮助你

编辑2(op编辑):

我想做的是写一些代码,这将给我这个功能。也就是说,一个基类有一个函数指针,可以设置为非成员函数(temp1)。。。或者从包含可调用的非静态成员的类继承(temp2)

class MyBase
{
public:

    void (*MyFunctionPointer)(int args);

};

class MyClass : public MyBase
{
public:

    void MyFunction(int args)
    {
    }

    MyClass()
    {
        MyFunctionPointer = &MyFunction;
    }
};

int _tmain(int argc, _TCHAR* argv[])
{
    MyClass temp;

    return 0;
}
class MyBase
{
public:

    void (*MyFunctionPointer)(int args);

};

class MyClass : public MyBase
{
public:

    void MyFunction(int args)
    {
    }

    void Bind(void (*myFunctionPointer)(int args))
    {
        MyFunctionPointer = myFunctionPointer;
    }
};

void MyFunction2(int args)
{
}

int _tmain(int argc, _TCHAR* argv[])
{
    MyBase temp1;
    temp1.MyFunctionPointer = &MyFunction2;        // <<-- ok
    temp1.MyFunctionPointer(0);

    MyClass temp2;
    temp2.Bind(&temp2.MyFunction);  // <<-- compile error
    temp2.MyFunctionPointer(0);

    return 0;
}
类MyBase
{
公众:
void(*MyFunctionPointer)(int参数);
};
类MyClass:公共MyBase
{
公众:
void MyFunction(int args)
{
}
void绑定(void(*myFunctionPointer)(int参数))
{
MyFunctionPointer=MyFunctionPointer;
}
};
void MyFunction2(int参数)
{
}
int _tmain(int argc,_TCHAR*argv[]
{
MyBase-temp1;

temp1.MyFunctionPointer=&MyFunction2;//@FredOverflow谢谢。我读了它,但还是不明白:(您是否尝试过
void(MyClass::*MyFunctionPointer)(int args);
?这将是对指针定义的更改,我无法更改(在实际代码中)。它驻留在基类中。有没有办法只更改我设置指针时使用的函数?在这种情况下,您需要一个
静态
方法。很抱歉,我没有得到它。我无法更改MyFunctionPointer的定义,但我可以更改MyFunction。我可以做些什么来让它工作吗?Thanks@Beakie:你不能改变它修复它?这是为什么?如果你不被“允许”修复该问题,那么你就无法修复该问题。你必须使
MyFunction
static
(在这种情况下,这实际上等同于使其成为非成员).Member函数是特殊的函数,由于它们对对象实例上下文的要求,与基本函数指针不兼容。使用上面编辑的示例,如果我要将MyBase中的定义更改为…void(MyBase::*MyFunctionPointer)(int args)为了让它指向MyClass中的方法,我应该在MyClass中将其设置为什么?Thanks@Beakie:你看过我链接到你的资料了吗?学习如何进行研究很重要。嗯,MyBase在我的代码中对MyClass一无所知……而且不能:(您试图实现什么?一种事件…由基类调用父类中存在的方法触发。我的实际代码中的类已模板化。请如何操作?