我必须声明Iter为typename但没有typedef。
然后,我不能Iter在我的template class声明中使用 => 令人沮丧 :( 请教
我如何使用Iter,否则请解释我为什么 C++ 如此讨厌......
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?为什么不? Iter内如何使用templateclass?(例如作为函数参数类型)
编辑
在指令typename Vec::iterator Iter;中,关键字typename说Vec::iterator的是一种类型(即不是静态成员函数/属性)。因此,Iter不是声明为类型,而是使用类型声明为变量Vec::iterator。