简体   繁体   English

如何将成员函数传递给采用函数指针的合成成员?

[英]How to pass member functions to composition's member that takes function pointers?

I have the below class that yields this error for the lines I commented: Description Invalid arguments 'Candidates are: Eigen::Matrix Forward_Euler(double ( )(double), double ( )(double), double (*)(double))' I'm getting confused when trying to search for a solution. 我有下面的类针对我注释的行产生此错误:说明无效参数'候选对象为:Eigen :: Matrix Forward_Euler(double( )(double),double( )(double),double(*)(double)) '试图寻找解决方案时,我感到困惑。

The signature for Forward Euler is: Forward Euler的签名是:

Eigen::MatrixXd Forward_Euler(double (*f)(double), double (*gleft)(double), double (*gright)(double))

And my class is: 我的课是:

class C
{
protected:
    A x;
    B y;
    double a;
    double b;
    Eigen::MatrixXd u;

public:
    double f(double x)
    {
        return a;
    }
    double gleft(double tau)
    {
        return b
    }
    double gright(double tau)
    {
        return a*b;
    }
    FD_Euro_Put(): x(), y(), a(0), b(0){}
    FD_Euro_Put(char y1, double y2, double y3, double y4, double x2,
            double x3, double x4, double x5, double x6, double x7):
            x(x1, x2, x3, x4, x5, x6, x7)
    {
        double Xleft = x1*x2;
        double Xright = x1*x3;
        double Tauf = x1*x1;
        double NN = floor((x1/x2);
        a = x1*x2 - 0.5;
        b = x1*x2 + 0.5;

        pde = HeatPDE(y1, NN, Xleft, Xright, Tauf, Alpha); //begin problem area
        u.resize(pde.N+1, pde.M+1); 
        if(fdtype == 'f')
            u = pde.Forward_Euler(&f, &gleft, &gright);
        else if(fdtype == 'b')
            u = pde.Backward_Euler(&f, &gleft, &gright);
        else if(fdtype == 'c')
            u = pde.Crank_Nicolson(&f, &gleft, &gright); //end problem area
        else
            cout << "Incorrect choice for finite difference type!" << endl;
    }

Member function pointers and function pointers are not the same thing. 成员函数指针和函数指针不是同一回事。 Member function pointers have a much more difficult job and need to deal with things like virtual functions and the this pointer. 成员函数指针的工作要困难得多,需要处理诸如虚函数和this指针之类的东西。

But look at the functions in your class! 但是,请看一下您班上的函数! They are not really object functions because they use nothing from the object. 它们并不是真正的对象函数,因为它们不使用任何对象。 You can either move them out of the class or make them static functions of the class. 您可以将它们移出类,也可以使其成为类的static函数。 If you do either of those things you can use them as function pointers. 如果您执行上述任一操作,则可以将它们用作函数指针。

I think you have to use a class template and static functions. 我认为您必须使用类模板和静态函数。 You can provide the arguments as template arguments. 您可以提供参数作为模板参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM