0

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.

The signature for Forward Euler is:

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;
    }
postelrich
  • 3,274
  • 5
  • 38
  • 65

1 Answers1

3

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.

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. 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.

Zan Lynx
  • 53,022
  • 10
  • 79
  • 131
  • Thank you for your response. Declaring them as static does work but the functions DO use the private members I just had taken out what the function actually returns when pasting the code here (I edited to reflect it now). Setting them static gets rid of my function pointer problem but now I can't access my object. – postelrich Dec 06 '12 at 21:20
  • @riotburn: Double check your PDE library. I doubt that the functions you're supposed to pass in are supposed to operate on anything that you didn't pass into the PDE functions. – Zan Lynx Dec 06 '12 at 21:55
  • class C is a pde that is being changed into the heat_pde. The pde class is used to solve the heat equation. So I need to pass the functions that are different in class c into my pde class in order to solve. – postelrich Dec 06 '12 at 22:00
  • @riotburn: But look at them: how is function f supposed to compute anything when the way you have it, it'll operate on a constant value every time it gets called? It does nothing with the 'x' passed to it. – Zan Lynx Dec 06 '12 at 22:26
  • Sorry, I was just using an arbitrary return but those functions return a more complicated function based on member variables from both class A and B. – postelrich Dec 06 '12 at 23:13