- #1
- 1,256
- 0
Hi, I was hoping a C++ guru could help me with a problem I'm having using the gnu science library (gsl) in my C++ code. Here is my issue:
Take for instance the Runge Kutter codes for solving differential equations. You need to supply the gsl routines for this a pointer to a function describing the derivatives of the system. In many cases I would like this function to be a member function of some class I have defined. However, since pointers to member functions are not the same as pointers to functions, when I try and do this I get an error.
This is annoying as it means all of these functions have to sit outside classes and especially annoying as these functions need to access many members of the classes they would ideally be members of in working out the derivatives. At present I need to use variables of global scope in order to get information into these functions which is annoying and ugly in a C++ context.
What I would love to know is how to make the gsl routines accept pointers to member functions, if this is at all possible. I know enough about this issue to realize what is causing the error when I give the gsl routines a pointer to a member function, but I have no idea how to resolve this issue, if it is even possible, which I certainly hope it is!
To be clear, here is a concrete example of my problem. Take the example ODE code http://www.gnu.org/software/gsl/manual/html_node/ODE-Example-programs.html" . The stepper function needs an object 'sys' defined by
gsl_odeiv_system sys = {func, jac, 2, &mu};
where func and jac are pointers to functions. The func given in this example for instance is
int
func (double t, const double y[], double f[],
void *params)
{
double mu = *(double *)params;
f[0] = y[1];
f[1] = -y[0] - mu*y[1]*(y[0]*y[0] - 1);
return GSL_SUCCESS;
}
What I want to do is provide a 'func' that is a pointer to a member function of an instantiated object. This is because the func needs to know a lot of information that is contained in that object. In this case a bunch of splines as well as some other functions. It is painful to have to make all of these have global scope just so this function can see them.
Can anyone suggest a solution? I would be most appreciative!
Take for instance the Runge Kutter codes for solving differential equations. You need to supply the gsl routines for this a pointer to a function describing the derivatives of the system. In many cases I would like this function to be a member function of some class I have defined. However, since pointers to member functions are not the same as pointers to functions, when I try and do this I get an error.
This is annoying as it means all of these functions have to sit outside classes and especially annoying as these functions need to access many members of the classes they would ideally be members of in working out the derivatives. At present I need to use variables of global scope in order to get information into these functions which is annoying and ugly in a C++ context.
What I would love to know is how to make the gsl routines accept pointers to member functions, if this is at all possible. I know enough about this issue to realize what is causing the error when I give the gsl routines a pointer to a member function, but I have no idea how to resolve this issue, if it is even possible, which I certainly hope it is!
To be clear, here is a concrete example of my problem. Take the example ODE code http://www.gnu.org/software/gsl/manual/html_node/ODE-Example-programs.html" . The stepper function needs an object 'sys' defined by
gsl_odeiv_system sys = {func, jac, 2, &mu};
where func and jac are pointers to functions. The func given in this example for instance is
int
func (double t, const double y[], double f[],
void *params)
{
double mu = *(double *)params;
f[0] = y[1];
f[1] = -y[0] - mu*y[1]*(y[0]*y[0] - 1);
return GSL_SUCCESS;
}
What I want to do is provide a 'func' that is a pointer to a member function of an instantiated object. This is because the func needs to know a lot of information that is contained in that object. In this case a bunch of splines as well as some other functions. It is painful to have to make all of these have global scope just so this function can see them.
Can anyone suggest a solution? I would be most appreciative!
Last edited by a moderator: