gpt4 book ai didi

c++ - 模板函数无法识别

转载 作者:行者123 更新时间:2023-11-30 03:13:49 30 4
gpt4 key购买 nike

我有课

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/
}

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

error: prototype for ‘void BaseStrategy::print_time(tm, std::chrono::duration<_Rep, _Period>)’ does not match any in class ‘BaseStrategy’ void BaseStrategy::print_time(tm t, std::chrono::duration fraction) ^~~~~~~~~~~~~~~

/home/yaodav/Desktop/git_repo/test/include/BaseStrategy.h:216:10: error: candidate is: template template void BaseStrategy::print_time(tm, std::chrono::duration) void print_time(tm t, chrono::duration fraction);

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

最佳答案

定义中模板参数的顺序

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

关于c++ - 模板函数无法识别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58411021/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com