- 论坛徽章:
- 0
|
我对template没有怎么写过,现在写了个template,但是怎么编译都不通过, 可能是不能这么用,所以想问问大家,我用的是gcc编译器,在hp-ux上的代码:
template<class T>
class ThreadLocal
{
//typedef std::map<pthread_t,T*> ThreadLocalMap;
public:
void put(T* elem)
{
//mutex protect.
//MutexGuard guard(mapMutex);
pthread_t tid = pthread_self();
//theadLocalMap.insert(std::map<pthread_t,T*>::value_type(tid,elem));
}
T* get()
{
//mutex protect.
//MutexGuard guard(mapMutex);
pthread_t tid = pthread_self();
//std::map<int,T> maptt;
std::list<T>::iterator iter;
return NULL;
}
void remove()
{
//mutex protect.
//MutexGuard guard(mapMutex);
pthread_t tid = pthread_self();
theadLocalMap.erase(tid);
}
private:
std::map<pthread_t,T*> theadLocalMap;
//MICOMT::Mutex mapMutex;
};
错误信息为:
In file included from dbtoolkit.h:5,
from dbtoolkit.cc:1:
threadlocal.h: In member function `T* ThreadLocal<T>::get()':
threadlocal.h:107: error: expected `;' before "iter"
threadlocal.h: In member function `T* ThreadLocal<T>::get() [with T = OraSession]':
dbtoolkit.cc:75: instantiated from here
threadlocal.h:107: error: dependent-name ` std::list<T,std::allocator<_CharT> >::iterator' is parsed as a non-type, but instantiation yields a type
threadlocal.h:107: note: say `typename std::list<T,std::allocator<_CharT> >::iterator' if a type is meant
如果我把std::list<T>::iterator iter; 修改为std::list<int>::iterator iter;编译通过, 如果直接std::list<T> list; 编译也是通过的,调用其他方法也行,就是只要使用了iterator,就编译报错,不光是list,map也是这样 |
|