收集的第二个模板; reverse_iterator没有编译

时间:2016-12-07 14:45:46

标签: c++ templates reverse-iterator

我正在转换两个现有函数,这两个函数检查迭代器是std::map中模板的最后一项和倒数第二项。 isLastItem模板已编译,isSecondLastItem失败并显示错误消息,我不明白。

导致错误的一行是typename T::reverse_iterator secondLastIt = collection.rbegin();

但模板和非模板版本看起来完全相同。

typedef std::map<time_t, std::string>  TInfoMap;

class MainClass
{
public:
    MainClass() {};
    void test();

private:
    /* Doesn't work */
    template <typename T>
    bool
    isSecondLastTemplate(const T &collection, const typename T::const_iterator &searchItem)
    {
        typename T::reverse_iterator secondLastIt = collection.rbegin();
        return true;
    }

    /* this works */
    bool
    isSecondLastNoTemplate(const TInfoMap &infoMap, const TInfoMap::const_iterator &searchItem)
    {
        TInfoMap::reverse_iterator secondLastIt = infoMap.rbegin();
        return true;
    }

    /* This works */
    template <typename T>
    bool
    isLastItem(const T &collection, const typename T::const_iterator &searchItem)
    {
        if (collection.size() == 1)
            return true;

        // point to last item
        typename T::const_iterator lastItem = collection.end();
        --lastItem;

        return searchItem == lastItem;
    }

private:
    TInfoMap   mInfoMap;
};


void
MainClass::test()
{
    mInfoMap[1] = "One";
    mInfoMap[2] = "Two";
    mInfoMap[3] = "Three";

    TInfoMap::iterator it = mInfoMap.begin();

    if (isLastItem(mInfoMap, it))
        printf("Last Item!\n");

    if (isSecondLastTemplate(mInfoMap, it))
        printf("Second Last!\n");

    if (isSecondLastNoTemplate(mInfoMap, it))
        printf("Second Last!\n");

}

int
main(int argc, char **argv)
{
    MainClass theStuff;
    theStuff.test();
}

编译错误:

In constructor ‘std::reverse_iterator<_Iterator>::reverse_iterator(const std::reverse_iterator<_Iter>&) 
                [with _Iter = std::_Rb_tree_const_iterator<std::pair<const long int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >, 
                _Iterator = std::_Rb_tree_iterator<std::pair<const long int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >]’:

junk3.cpp:19:   instantiated from here
error: no matching function for call to ‘std::_Rb_tree_iterator<std::pair<const long int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >::_Rb_tree_iterator(std::_Rb_tree_const_iterator<std::pair<const long int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >)’
candidates are: 
  std::_Rb_tree_iterator<_Tp>::_Rb_tree_iterator(std::_Rb_tree_node<_Tp>*) [with _Tp = std::pair<const long int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >]
  std::_Rb_tree_iterator<_Tp>::_Rb_tree_iterator() [with _Tp = std::pair<const long int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > >]

  std::_Rb_tree_iterator<std::pair<const long int, std::basic_string<char, std::char_traits<char>, 
       std::allocator<char> > > >::_Rb_tree_iterator(const std::_Rb_tree_iterator<std::pair<const long int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >&)

1 个答案:

答案 0 :(得分:0)

const_reverse_iterator collection

以来,您需要const
  template <typename T>
  bool
    isSecondLastTemplate(const T &collection, const typename T::const_iterator &searchItem)
  {
    typename T::const_reverse_iterator secondLastIt = collection.rbegin();
                ^^^^^^^^^^^^^^^^^^^^^^
    return true;
  }