模板函数无法识别

template function doesn't recognize

本文关键字:识别 函数      更新时间:2023-10-16

我有一个类

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::p rint_time(tm, std::chrono::d uration<_Rep, _Period>(' 与类中的任何内容都不匹配 'BaseStrategy' void BaseStrategy::p rint_time(tm t, 标准::时间::d尿分数( ^~~~~~~~~~~~~~~

/home/yaodav/Desktop/git_repo/test/include/BaseStrategy.h:216:10: 错误:候选为:模板模板无效 BaseStrategy::p rint_time(tm, std::chrono::d uration( 空隙print_time(TM T,计时::d尿分数(;

为什么会发生此错误?以及如何修复它

定义中模板参数的顺序

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);