将函数作为参数传递的范围

Scope of passing function as a parameter

本文关键字:范围 参数传递 函数      更新时间:2023-10-16

我的代码遇到了一些问题。

我有一堂课:

class SingleNeuron {
public:
    double hh_function (double t , double u);
    double nn_function (double t , double u);
    double zz_function (double t , double u);
    double VV_function (double t , double u);
    void calculating_next_step( double t0, double dt ) {
        hh=rk4 ( t0, hh, dt, hh_function );
        nn=rk4 ( t0, nn, dt, nn_function );
        zz=rk4 ( t0, zz, dt, zz_function );
        VV=rk4 ( t0, zz, dt, VV_function );
 }
};
/*the rk4 is a function of someone else:*/
double rk4 ( double t0, double u0, double dt, double f ( double t, double u ) );

我在 hh、nn、zz 、VV 中遇到了问题:

error C3867: 'SingleNeuron::hh_function': function call missing argument list;
use '&SingleNeuron::hh_function' to create a pointer to member

我试图将其更改为 &SingleNeuron::hh_function,问题是:

error C2664: 'rk4' : cannot convert parameter 4 from
'double (__thiscall SingleNeuron::* )(double,double)'
to 'double (__cdecl *)(double,double)'
1> There is no context in which this conversion is possible

不能将成员函数作为函数作为函数指针传递,因为没有实例的成员函数毫无意义。

如果您的hh_function仅取决于其参数。可以将其声明为静态成员函数。这样,您可以将其作为普通函数指针传递。