传递函数作为参数的范围

时间:2014-12-14 17:23:32

标签: c++

我的代码存在问题。

我有一个班级:

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

1 个答案:

答案 0 :(得分:1)

您不能将成员函数作为函数指针传递,因为如果没有实例,成员函数就没有意义。

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