【发布时间】:2012-10-19 07:59:40
【问题描述】:
我必须将Iter 声明为typename,但没有typedef。
然后,我不能在我的template class 声明中使用Iter => 令人沮丧:(
请教我如何使用Iter,否则请解释我为什么C++如此nasty...
template <typename T>
struct MyContainer
{
typedef std::vector<T> Vec;
typename Vec::iterator Iter;
void Fo (typename std::vector<T>::iterator); //ok
typename std::vector<T>::iterator Ba(); //ok
void Foo (Iter); //error: 'Iter' is not a type
Iter Bar (); //error: 'Iter' does not name a type
}; //(gcc 4.1.2)
- 为什么
typedef不能用来声明Iter?为什么不呢? - 如何在
templateclass内使用Iter? (例如作为函数参数类型)
EDIT
在指令typename Vec::iterator Iter; 中,关键字typename 表示Vec::iterator 是一种类型(即不是静态成员函数/属性)。因此,Iter 没有声明为类型,而是使用类型Vec::iterator 声明为变量。
【问题讨论】:
标签: c++ templates typedef nested-class typename