将公共但非静态的成员函数与ALGLIB集成



我正在尝试集成一个类的成员函数,需要一些帮助!我不能将函数声明为static,因为此函数使用非静态私有成员(更具体地说,使用另一个类的私有成员(我正在使用C++17,无法降级

我在下面写了一个模仿我的问题的最小(非(工作示例。目的是找到应该放什么,而不是四个问号"???"在CCD_ 1中。

alglib::autogkintegrate函数有三个参数:

  1. 一个包含积分的当前值、积分边界和其他东西的"状态"?(
  2. 要集成的函数f必须是原型void f(double x, double xminusa, double bminusx, double &y, void *ptr)
  3. 可选指针CCD_ 5,其包含用于函数CCD_

这是一个不起作用的例子:

#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;
}

如果我将函数f声明为static(并在f中删除this->m_toto(,那么我可以使用alglib::autogkintegrate(s, ????, params)1来集成f。因此,真正的问题是访问函数f

我试图定义一个指向成员函数的指针(解释#include <functional>(,但未能在alglib::autogkintegrate(s, ????, params);中使用它

重复我的问题:我想在C++中使用"Alglib"集成类的成员函数

有用的链接:

  1. https://www.alglib.net/translator/man/manual.cpp.html#gs_using第8.5节
  2. https://www.alglib.net/translator/man/manual.cpp.html#example_autogk_d1

p。S.:我也在http://forum.alglib.net/viewtopic.php?f=2&t=4352,这是Alglib的专用论坛(但看起来比Stack Overflow活跃得多,因此我发了两篇帖子。如果我在那里得到答案,我会复制粘贴到这里,反之亦然(

我在http://www.newty.de/fpt/callback.html使用全局变量(对不起…(。如果有人需要,我会在这里发布适合我问题的代码:

#include <functional>
#include <iostream>
#include <cmath>
#include "integration.h"
class Foo;
class Bar;
void* pt2Object; // global variable which points to an arbitrary object
/*****************************
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);
// Wrapper
static void Wrapper_To_Call_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);
// y = exp(x)/(p1 * p2);
}
void Foo::Wrapper_To_Call_f(double x, double xminusa, double bminusx, double &y, void *ptr)
{
// explicitly cast global variable <pt2Object> to a pointer to TClassB
// warning: <pt2Object> MUST point to an appropriate object!
Foo* mySelf = (Foo*) pt2Object;
// call member
mySelf->f(x, xminusa, bminusx, y, ptr);
}
/*****************************
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();
// Create Foo
Foo myFoo(b, 1);
// Assign global variable which is used in the static wrapper function
// important: never forget to do this!!
pt2Object = (void*) &myFoo;
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, Foo::Wrapper_To_Call_f, params);
alglib::autogkresults(s, v, rep);
std::cout << v << std::endl;
return 0;
}

最新更新