将成员函数传递给静态函数

将成员函数传递给静态函数

本文介绍了将成员函数传递给静态函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Math.h

Math.h

class Math
{
    public:
    static double integrate(double (*f)(double x), double a, double b);
};



Math.cpp



Math.cpp

#include "Math.h"

double Math::integrate(double (*f)(double x), double a, double b)
{
    int m = 1000;
    double dt = (b-a)/m;
    double c = 0;

    for(int i = 0; i<m;>    {
        c += (*f)(a+i*dt);
    }

    return c/m;
}



型号.cpp



Model.cpp

...
double Model::i_L_charging(double t)
{
    return V_in/(R_L+RDSON) - V_in/(R_L+RDSON)*exp(-t/tau_charging());
}

double Model::I_d_avg()
{
    return math.integrate(&i_L_charging, 0.0, T_charging()); //Error is here
}
...



缩短的错误是(GNU GCC编译器):

||在成员函数`double Model :: I_d_avg()'':|

| 45 |错误:ISO C ++禁止使用不合格或带括号的非静态成员函数的地址来形成指向成员函数的指针.说`& Model :: i_L_charging''|

| 45 |错误:没有匹配函数可调用`Math :: integrate(double(Model :: *)(double),double,double)''|

| 5 |注意:候选对象是:静态double Math :: integrate(double(*)(double),double,double)|

|| ===构建完成:2个错误,0个警告=== |


就像测试一样,我将"sqrt"放入函数中,而不是"i_L_charging",并且效果很好.这里发生了什么?

感谢您的帮助.

问候,
Johnny



The shortend error is (GNU GCC Compiler):

||In member function `double Model::I_d_avg()'':|

|45|error: ISO C++ forbids taking the address of an unqualified or parenthesized non-static member function to form a pointer to member function. Say `&Model::i_L_charging''|

|45|error: no matching function for call to `Math::integrate(double (Model::*)(double), double, double)''|

|5|note: candidates are: static double Math::integrate(double (*)(double), double, double)|

||=== Build finished: 2 errors, 0 warnings ===|


Just as a test I put "sqrt" in as the function instead of "i_L_charging" and it works perfectly. What is going on here?

Your help is appreciated.

Regards,
Johnny

推荐答案


这篇关于将成员函数传递给静态函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 14:44