2009-06-26 159 views
0

下面的代碼是給編譯錯誤在Visual Studio 2009年C++編譯錯誤

#include <iterator> 
#include <vector> 

template <class T1, class T2 > 
class A 
{ 
public: 

    typename std::vector<std::pair<T1,T2> >::iterator iterator; 
    std::pair<iterator, bool > foo(const std::pair<T1 ,T2> &value_in); 
}; 

任何人都可以扔在這一些輕?這是錯誤。

error C2327: 'A<T1,T2>::iterator' : is not a type name, static, or enumerator 

回答

9

這聲明iterator是一個變量(而不是類型):

typename std::vector<std::pair<T1,T2> >::iterator iterator; 

你說這個?

typedef typename std::vector<std::pair<T1,T2> >::iterator iterator; 

更多信息:如果您想了解什麼typename呢,讀了關於dependent和非依賴名之間的差別。如果您的類型與特定容器密切相關,那麼該容器的typedef可能很有用,因爲STL模式使用了許多可以輕鬆訪問的嵌套typedef(下面的V::value_type)。這還有一個好處,就是隨着代碼的發展需要更少的更改,例如,使用不同的分配器(第二個模板參數),只需要一次編輯。

template<class T1, class T2> 
struct A { 
private: 
    // you may or may not want to expose these convenience types 
    typedef std::pair<T1, T2> P; 
    typedef std::vector<P> V; 

public: 
    typedef typename V::value_type value_type; 
    typedef typename V::iterator iterator; 
    std::pair<iterator, bool> foo(value_type const& value_in); 
}; 
0

你需要的typedef,不是類型名