2012-09-24 41 views
2

可能重复:
Where and why do I have to put the “template” and “typename” keywords?如何为泛型类型的向量声明一个迭代器?

我声明为是一个通用类型的矢量的迭代器的问题。 代码如下:

template <class T> void print(const vector<T>& V) 
    { 
     vector<T>::const_iterator i; 
    } 

下面返回预期的误差;于我之前'。 如果我特别说明,将不会有错误vector<int>::const_iterator i;

有没有办法解决这个问题?

+0

'类型名称矢量 ::为const_iterator我;' – hmjd

回答

3

const_iterator是在此上下文中的从属名称,因为它取决于T。除非您明确使用关键字typename来限定它,否则不会假定其类型。

template <class T> void print(const vector<T>& V) 
{ 
    typename vector<T>::const_iterator i; 
} 
1

你需要这样做:

template <class T> void print(const vector<T>& V) 
    { 
     //T is a dependant type so needs typename 
     typename vector<T>::const_iterator i; 
    }