gpt4 book ai didi

c++ - 如何使用 odeint 的标签系统为各种步进器类型做特定的工作

转载 作者:行者123 更新时间:2023-11-30 04:10:40 27 4
gpt4 key购买 nike

我有一个模板化类,它可以使用 odeint 的步进器类执行一些工作,我希望它是针对每个步进器类别的特定(不同)工作。

/// header file ///

template<class Stepper>
class foo {
typedef typename boost::numeric::odeint::unwrap_reference< Stepper >::type::stepper_category stepper_category;
void do_work(double param);
// specific functions for various stepper types
void do_specific_work(double param, stepper_category);
}

/// cpp file ///
template<class Stepper>
void foo<Stepper>::do_work(double param)
{
do_specific_work(param, stepper_category());
}

// actual implementation of work for any stepper (i.e. exposing the basic functionality of stepper_tag)
template<class Stepper>
void foo<Stepper>::do_specific_work(double param, boost::numeric::odeint::stepper_tag)
{ ... }

// actual implementation of work for dense output stepper (i.e. exposing the functionality of dense_output_stepper_tag)
template<class Stepper>
void foo<Stepper>::do_specific_work(double param, boost::numeric::odeint::dense_output_stepper_tag)
{ ... }

问题是我收到以下编译器错误:

error C2244: 'foo<Stepper>::do_specific_work' : unable to match function definition to an existing declaration `

我尝试用与 integrate_adaptive 等方法相同的方式来做已实现,与我的情况不同的是那些是独立函数(不是任何类的成员)并且不需要前向声明。如何修改代码来实现我所需要的?提前致谢!

最佳答案

您需要为特定类别提供显式重载:

template<class Stepper>
class foo {
typedef typename boost::numeric::odeint::unwrap_reference< Stepper >::type::stepper_category stepper_category;

// ...


void do_specific_work(double param, stepper_tag );
void do_specific_work(double param, dense_output_stepper_tag );
};

template< class Stepper >
void foo< Stepper >::do_specific_work( double param , stepper_tag ) { ... };

template< class Stepper >
void foo< Stepper >::do_specific_work( double param , dense_output_stepper_tag ) { ... };

您有一个声明 do_specific_work( double param , stepper_category ) 和几个定义。您的原型(prototype)现在与定义不匹配。

关于c++ - 如何使用 odeint 的标签系统为各种步进器类型做特定的工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20555197/

27 4 0