我需要解决用odeint库设置的ODE方程。 “main.cpp”文件只是为了调用计算(稍后会扩展,这里是简单版本以保持代码清洁)
main.cpp
#include "calc.h"
int main()
{
calc c;
c.startC();
return 0;
}
这是calc头,starC();是公开的。
calc.h
#ifndef MAIN_H
#define MAIN_H
#include <iostream>
#include <boost/numeric/odeint.hpp>
#include <math.h>
using namespace std;
using namespace boost::numeric::odeint;
typedef std::vector< double > vector_type;
class calc
{
public:
int main();
void startC();
private:
void solveODE(const vector_type &y , vector_type &ODE , const double t );
void printR();
};
#endif // MAIN_H
这是主要的计算部分。此处也出现错误:
calc.cpp
include <iostream>
#include <boost/numeric/odeint.hpp>
#include <math.h>
#include "calc.h"
using namespace std;
using namespace boost::numeric::odeint;
const double g = 0.15;
void calc::solveO( const vector_type &y , vector_type &ODE , const double t )
{
dy[0] = y[1];
dy[1] = -y[0] - g*y[1];
}
void calc::printR(const vector_type &y, const double t)
{
cout << t << endl;
cout << y[0] << endl;
}
void calc::startC()
{
const double dt = 0.1;
typedef runge_kutta_dopri5<vector_type> stepper_type;
vector_type y(2);
//Initial conditins
y[0]= 1.0;
y[1]= 0.0;
//Initializing of ODE solver
integrate_const(make_dense_output<stepper_type>( 1E-12, 1E-6 ),
this->*solveO, y, 0.0, 1.0, dt , printResults); //ERROR HERE
}
int calc::main()
{
return 0;
}
此操作以“初始化ODE解算器”的级别上的错误结束:
'((calc*)this)->calc::solveO' cannot be used as a member pointer,
since it is of type '<unresolved overloaded function type>'
这次电话有什么问题:
this->*solveO
在“integrate_const”中调用solveO的正确方法是什么?
修改 感谢@ tobi303和@rbelli的帮助,它现在有效。在这里我发布总结,TL:DR解释:
最简单的解决方案是使solveO和printR自由功能并调用:
integrate_const(make_dense_output<stepper_type>( 1E-12, 1E-6 ), solveO, y, 0.0, 1.0, dt , printR);
答案 0 :(得分:1)
如果integrate_const
采用函数指针,则无法传递成员函数指针。成员函数指针与函数指针不同,因为它们隐含地需要this
作为参数。
由于你不使用solveO
中的任何类成员,你可以简单地使它成为一个自由函数:
void solveO( const vector_type &y , vector_type &ODE , const double t )
{
dy[0] = y[1];
dy[1] = -y[0] - g*y[1];
}