模板功能无法识别

时间:2019-10-16 10:20:54

标签: c++ templates

我上课

template <class T>
class BaseStrategy
{
template<typename Period, typename Rep>
void print_time(tm t, chrono::duration<Rep, Period> fraction);
}

和实现是(在同一.h文件中)

template <typename T>
template <typename Rep, typename Period >
void BaseStrategy<T>::print_time(tm t, std::chrono::duration<Rep, Period> fraction)
{
    /some code/
}

但是当我编译代码时,出现以下错误:

  

错误:“ void BaseStrategy :: print_time(tm,   std :: chrono :: duration <_Rep,_Period>)’与课程中的任何内容都不匹配   “ BaseStrategy”无效BaseStrategy :: print_time(tm t,   std :: chrono :: duration分数)         ^ ~~~~~~~~~~~~~~

     

/home/yaodav/Desktop/git_repo/test/include/BaseStrategy.h:216:10:   错误:候选者是:模板模板无效BaseStrategy :: print_time(tm,std :: chrono :: duration)        void print_time(tm t,chrono :: duration fraction);

为什么会发生此错误?以及如何解决

1 个答案:

答案 0 :(得分:2)

模板参数在定义中的顺序

template <typename Rep, typename Period >
void BaseStrategy<T>::print_time(tm t, std::chrono::duration<Rep, Period> fraction)

与声明中模板参数的顺序不符

template<typename Period, typename Rep>
void print_time(tm t, chrono::duration<Rep, Period> fraction);

要么写

template<typename Rep, typename Period>
void print_time(tm t, chrono::duration<Rep, Period> fraction);

或(更加令人困惑)

template<typename Period, typename Rep>
void print_time(tm t, chrono::duration<Period, Rep> fraction);