【问题标题】:template function doesn't recognize模板函数无法识别
【发布时间】:2020-02-13 02:23:35
【问题描述】:

我有课

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’ void BaseStrategy::print_time(tm t, std::chrono::duration 分数) ^~~~~~~~~~~~~~~~~~

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

为什么会出现这个错误?以及如何解决它

【问题讨论】:

    标签: c++ templates


    【解决方案1】:

    定义中模板参数的顺序

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

    【讨论】:

    • tnx,我没看到
    猜你喜欢
    • 2013-07-10
    • 1970-01-01
    • 2012-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-21
    • 1970-01-01
    相关资源
    最近更新 更多