上使用g++版本4.8.4
我有一个类似于这个的代码:
#include <iostream>
template<typename T> class Functor
{
T *pthis;
void (T::*fun)(void);
public:
Functor(T* punteroThis, void (T::*funcion)() ) : pthis(punteroThis), fun(funcion) { }
void operator() (void);
};
template<typename T>
void Functor<T>::operator() ()
{
(pthis->*fun)();
}
template<typename T> class myTimer
{
/*
omitted 'typename' so that T inside task
is the same as T in class myTimer
*/
template<T> class task
{
unsigned id;
bool active;
Functor<T> fun;
unsigned interval;
public:
task(unsigned id_, bool active_, Functor<T> fun_, unsigned interval_) : id(id_), active(active_), fun(fun_), interval(interval_) { }
};
void print(myTimer::task<T> const& t); // << this is not working
};
template<typename T>
void myTimer<T>::print(myTimer::task<T> const& t)
{
std::cout << "id = " << t.id << 'n';
std::cout << "active = " << t.active << 'n';
std::cout << "interval = " << t.interval << std::endl;
}
调用编译器时
g++ -Wall -g -std=c++11 myTimer.h -c
我在print()
:的声明和定义中都有以下错误
error: type/value mismatch at argument 1 in template parameter list for
‘template<class T> template<T <anonymous> > class myTimer<T>::task’
error: expected a constant of type ‘T’, got ‘T’
我也尝试了typename
,并得到了相同的输出。我不知道怎么解决。知道发生了什么吗?
我在Linux平台
此代码不正确:
/*
omitted 'typename' so that T inside task
is the same as T in class myTimer
*/
template<T> class task
如果您希望任务中的T与包含范围中的T相同,只需完全省略template<T>
即可。
然后你有:
void print(myTimer::task<T> const& t); // << this is not working
你想要的地方:
void print(myTimer::task const& t);