【问题标题】:C++, iterator to std::mapC++,到 std::map 的迭代器
【发布时间】:2011-05-16 18:27:39
【问题描述】:

如何声明一个迭代器

std::map <T, Point <T> *> ,

地点:

template <typename T>
struct TList
{
    typedef std::vector < std::map <T, Point <T> *> >  Type;
};

在下面的代码中

int main ()
{
    ....
    std::map <T, Point <T> *> ::iterator i_map;  //Error
    ...
}

g++ 显示此错误:

error: dependent-name `  std::map<T,Point<T>*,std::less<_Key>,std::allocator<std::pair<const T, Point<T>*> > >::iterator' is parsed as a non-type, but instantiation yields a type
note: say `typename  std::map<T,Point<T>*,std::less<_Key>,std::allocator<std::pair<const T, Point<T>*> > >::iterator' if a type is meant

【问题讨论】:

    标签: c++ iterator stdmap


    【解决方案1】:

    typename 用作:

      typename std::map<T, Point <T> *>::iterator i_map;
    //^^^^^^^^ here!
    

    因为iterator 是一个依赖名称(因为它取决于地图的type 参数T),所以这里需要typename

    详细解释请阅读此常见问题解答:

    Where and why do I have to put the "template" and "typename" keywords?

    【讨论】:

      【解决方案2】:

      typename std::map &lt;T, Point &lt;T&gt; *&gt; ::iterator i_map; 有效吗?

      【讨论】:

        【解决方案3】:

        typename TList&lt;T&gt;::Type::value_type::iterator 呢?

        【讨论】:

          【解决方案4】:

          在错误行前加上“typename”:std::map &lt;T, Point &lt;T&gt; *&gt; ::iterator i_map;

          例子:

          typename vector<T>::iterator vIdx; 
          

          // On your case : typename std::map &lt;T, Point&lt;T&gt;*&gt;::iterator i_map;

          vIdx= find(oVector->begin(), oVector->end(), pElementToFind); //To my case
          

          【讨论】:

            猜你喜欢
            • 2012-09-30
            • 2014-05-27
            • 2011-01-30
            • 1970-01-01
            • 2011-10-28
            • 2021-05-08
            • 2012-08-28
            • 2013-10-25
            • 1970-01-01
            相关资源
            最近更新 更多