簡體   English   中英

C ++編譯錯誤

[英]c++ compilation error

以下代碼在Visual Studio 2009中給出了編譯錯誤。

#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

這將iterator聲明為變量(不是類型):

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

你是這個意思嗎

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

更多信息:如果您想了解什么typename呢,讀了大約之間的差異依賴和非依賴的名字。 如果您的類型與特定的容器緊密相關,則該容器的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);
};

您需要typedef,而不是typename

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM