vector <T> :: iterator - 无效?

Jac*_*cob 3 c++ templates vector

可能重复:
g ++"不是类型"错误

以下内容无法编译:

1    template<typename T>
2    void foo(std::vector<T>::iterator & i)
3    {  
4    }
Run Code Online (Sandbox Code Playgroud)

在Visual Studio上,我收到以下错误:

>(2) error C2065: 'i' : undeclared identifier
>(4) warning C4346: 'std::vector<_Tp>::iterator' : dependent name is not a type
     prefix with 'typename' to indicate a type
>(4) error C2182: 'foo' : illegal use of type 'void'
>(4) error C2998: 'int foo' : cannot be a template definition
Run Code Online (Sandbox Code Playgroud)

GMa*_*ckG 14

std::vector<T>::iterator是一种依赖于模板参数的类型,即T.因此,您应该使用它作为前缀typename:

template<typename T>
void foo(typename std::vector<T>::iterator & i)
{  
}
Run Code Online (Sandbox Code Playgroud)

这是一个解释.

  • @Jacob:接受的答案不受限制:http://stackoverflow.com/faq (2认同)