【问题标题】:c++ odeint - integrate a static ode [closed]c++ odeint - 集成静态 ode [关闭]
【发布时间】:2014-08-18 18:05:01
【问题描述】:

我的 c++ 程序有问题,而且 C++ 还不够好...请您帮我解决这个问题吗?

所以我使用 boost::odeint。我有我的 ode 和集成函数,来自库,在一个类 C 中。但是我有一个错误,“ode”必须是非静态的(错误:对非静态成员函数的引用必须是称为)。即使integrate和ode是同一个类。

如何使我的集成​​函数使用静态 ode?​​p>

提前谢谢你!

class C
{
public :
typedef boost::array< double , 3 > state_type;
C(const state_type &B);
~C();

const void ode( const state_type &x , state_type &dxdt , double t);
void write_out( const state_type &x , const double t );
void integration();

private :
const state_type x;
};

void C :: ode(const state_type &x , state_type &dxdt , double t)
{ (...) }

void C :: integration()
{
    out.open( "results.txt" );
    integrate_const( runge_kutta_fehlberg78< state_type >() , ode,
                    x , 0.0 , 10.0 , 0.01, write_out );
    out.close();
}

## Main.cpp

using namespace std;
using namespace boost::numeric::odeint;


int main(int argc, const char * argv[])
{

C::state_type x = {{ 10.0 , 10.0 , 10.0 }};
C Com1 (x);

Com1.integration();

return 0;
}

【问题讨论】:

  • 请勿在标识符中使用美元符号。
  • 将 const 移到 ode 函数的末尾:void ode( const state_type &x , state_type &dxdt , double t) const;
  • 我不知道你想在这里做什么: const void :: ode(const state_type &x , state_type &dxdt , double t ) - 这不是有效的 C++ 语法
  • const void 返回规范应该做什么?除此之外,const void :: ode(...) 应该是 void C::ode(...) 用于定义类成员函数。
  • @cppguy:你确定吗?我认为const void 可能是允许的(尽管在TMP 之外无用),并且声明在全局命名空间中声明了一个函数ode。我认为这是有效的 C++ 语法(虽然不是他想要的)

标签: c++ function templates boost static


【解决方案1】:

我看到两个问题:

首先,去掉x类成员定义中的const:

class C
{
    /* ... */
    state_type x;
}

其次,可以根据成员函数定义 ode,但您需要将其绑定到当前对象。通过方括号运算符operator() 定义 ode 要容易得多。所以将 ode 替换为

void operator()( const state_type &x , state_type &dxdt , double t);

并通过调用 ode

integrate_const( runge_kutta_fehlberg78< state_type >() , boost::ref( *this ) ,
                x , 0.0 , 10.0 , 0.01, write_out );

【讨论】:

  • 谢谢这个问题解决了!只需超载即可使其正常工作...
猜你喜欢
  • 1970-01-01
  • 2017-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-17
  • 2020-06-05
  • 2017-01-28
  • 1970-01-01
相关资源
最近更新 更多