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

时间:2013-12-12 22:03:34

标签: c++ boost odeint

我有一个模板化的类,可以使用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等方法的方式相同,与我的情况不同的是,这些是独立的函数(不是任何类的成员)而不是需要前瞻声明。如何修改代码来实现我的需求? 提前谢谢!

2 个答案:

答案 0 :(得分:1)

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

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)和几个定义。你的原型现在与定义不符。

答案 1 :(得分:1)

// actual implementation of work for any stepper (i.e. exposing the basic functionality of stepper_tag)

如果您希望此方法与任何stapper_tag一起使用,则必须使用函数模板:

template<class StepperTag>
void do_specific_work(double param, StepperTag stepper);

实施:

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

template<class Stepper>
void foo<Stepper>::do_specific_work(double param, boost::numeric::odeint::dense_output_stepper_tag stepper)
{ ... }
相关问题