Problems with member function reference

Hello,

I am doing numerical work and am using the GSL (GNU scientific library) to help me with my endeavors. I recently had some code working procedurally, and then switched over to object oriented for the purposes of code reuse. The problem is I am having trouble using a library function that was working fine before.

GSL defines a data type or class called gsl_function. It has has two members, one which is a pointer to a function and one which is a general void pointer (for the purposes of taking in parameters). The function pointer has prototype given by

double (*function) (double x, void * params).

So you define a function f that returns a double and takes in a double and a void pointer, and then you write

gsl_function F;
F.function = &f;

I got this to work in a procedural setting. Now I am trying to do this in a class. My class has a member called myODE_dydx_ which has exactly the form I described for f. Then inside another member function called func, I have

gsl_function F;
F.function = &LensSolver::myODE_dydx_;

When I put this in, I get the error:

error: cannot convert ‘double (LensSolver::*)(double, void*)’ to ‘double (*)(double, void*)’ in assignment.

However, when I leave out the "LensSolver::" I still get this error in addition to another one, which I googled and found out is related to how member functions are addressed. I don't have the faintest clue what its telling me, much less how to solve it. Any help?
Try using a static member function. Non-static member functions work differently and their pointers are not interchangeable with regular function pointers.

This is because they receive and implicit additional parameter to the object on which they are being invoked. So they can't be invoked independent of the object they are called from.
Last edited on
Hey,

thanks for the help. Your advice worked to remove the problem. However, it unfortunately introduces a new one. The function in question f is actually a pretty trivial function, it just reorganizes some stuff and then calls another function call it f2, which isn't static, and can't be static because it uses some other non static members of the class. Is there anyway to break this static/non-static chain, i.e. how can I ultimately use a static function to deal with non-static members? The only solution I can think of right now is to actually pass the "this" pointer into myODE_dydx_ but this seems a bit ugly.
To be honest passing the this pointer makes sense to me. You are giving the library a static function that is designed to receive a pointer to the actual data (in the form of a void*). That data would logically be your object. Hence the this pointer.

The problem is you are interfacing with a library that was designed to work with a non-object oriented language. And you want to use it in an object orientated way. So the match is not likely to be as elegant as you might want.
Hey Galik, I will probably follow this then. Thanks very much for all your help.
For callback functions, I find the boost::function and boost::bind libraries the most useful. You could
consider using a boost::function<double(double)>.

1
2
3
4
5
6
7
8
9
10
11
12
// assume:
class MyClass {
  public:
      double MyMemberFunction( double );
};

gsl_function F;
f.function = boost::bind( &MyClass:MyMemberFunction, &instaceOfTypeMyClass, _1 );


// Then call it:
f.function( 3.14 );

Topic archived. No new replies allowed.