2010-08-09 90 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

相關問題