简体   繁体   中英

template function doesn't recognize

I have a class

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

and the implemention is (in the same.h file)

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

but when I compile the code I'm getting the following error:

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

why is this error occurred? and how to fix it

The order of the template arguments in the definition

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

does not correspond to the order of the template parameters in the declaration

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

Either write

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

or (more confusing)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM