![](/img/trans.png)
[英]c++ template - type/value mismatch - Instantiate base class template with a type dependent on derived class
[英]Type/value mismatch in member function of C++ template class
我有與此類似的代碼:
#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平台上使用g ++版本4.8.4
此代碼不正確:
/*
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.