2010-08-09 89 views
3

可能重复:
g++ “is not a type” errorvector <T> ::迭代器 - 无效?

下无法编译:

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

在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 
+1

发现这个[复制](http://stackoverflow.com/questions/1301380/g-is-not-a-type-error) ,还有更多,但我找不到它们。 :S – GManNickG 2010-08-09 20:52:41

回答

13

std::vector<T>::iterator是一个从属于的模板参数,即T。因此,您应该用它的前缀typename

template<typename T> 
void foo(typename std::vector<T>::iterator & i) 
{ 
} 

Here's an explanation.

+0

+1比我快。 – 2010-08-09 20:51:25

+0

@Fred:我很忙,所以我很快就接受了答案。 :P – GManNickG 2010-08-09 20:53:23

+0

哈哈,明天我会接受你的答案然后:)(这样你就可以得到+15) – Jacob 2010-08-09 20:54:12

相关问题