I am trying to integrate a member function of a class, and needs some help! I cannot declare the function static because this function uses non-static private members (more specifically use a private member which is another class). I am using C++17 and cannot downgrade
I write below a Minimum (Non) Working Example that mimic my problem. The aim is to find what to put instead of the four question marks '????' in alglib::autogkintegrate(s, ????, params)
.
The alglib::autogkintegrate
function takes three arguments:
- a 'state' which contains the current value of the integration, integral boundaries, other stuff?)
- The function
f
to integrate which must be of the prototypevoid f(double x, double xminusa, double bminusx, double &y, void *ptr)
- an optional pointer
void *ptr
which contains extra parameters for the functionf
Here is the non working example:
#include <functional>
#include <iostream>
#include <cmath>
#include "integration.h"
class Foo;
class Bar;
/*****************************
Class Foo
*****************************/
class Foo
{
public:
// Constructor
Foo(Bar *b, int toto);
// Function to integrate
void f(double x, double xminusa, double bminusx, double &y, void *ptr);
private:
Bar *m_bar;
int m_toto;
};
Foo::Foo(Bar *b, int toto) : m_bar(b), m_toto(toto)
{}
void Foo::f(double x, double xminusa, double bminusx, double &y, void *ptr)
{
double *param = (double *) ptr;
double p1 = param[0];
double p2 = param[1];
y = exp(this->m_toto*x)/(p1 * p2);
}
/*****************************
Class Bar
*****************************/
class Bar
{
friend Foo;
public:
// Constructor
Bar();
private:
int m_a, m_b;
};
Bar::Bar() : m_a(2), m_b(5)
{}
/*****************************
Main program
*****************************/
int main(int argc, char *argv[])
{
Bar* b = new Bar();
Foo f(b, 87);
double arrayParams[2] = {1, 2};
double (*params)[2] = &arrayParams;
alglib::autogkstate s;
double v;
alglib::autogkreport rep;
alglib::autogksmooth(0, 1, s);
alglib::autogkintegrate(s, ????, params);
alglib::autogkresults(s, v, rep);
return 0;
}
If I declare the function f
as static (and remove this->m_toto
in f
), then I can integrate f
by using alglib::autogkintegrate(s, Foo::f, params);
. So the problem is really to get access to the function f
.
I tried to define a pointer to member function (which explains #include <functional>
) but failed to use it in alglib::autogkintegrate(s, ????, params);
To repeat my question: I want to integrate a member function of a class using ``Alglib``` in C++
Useful links:
- https://www.alglib.net/translator/man/manual.cpp.html#gs_using section 8.5
- https://www.alglib.net/translator/man/manual.cpp.html#example_autogk_d1
P.S.: I also posted this question at http://forum.alglib.net/viewtopic.php?f=2&t=4352 which is a dedicated forum to Alglib (but looks much less active than Stack Overflow, hence my double post. If I get an answer there, I will copy-paste here, and vice-versa)