【问题标题】:Error C2676 in VC2012 and C2784 in VC2005: reverse_iterator for map of template classesVC2012 中的错误 C2676 和 VC2005 中的 C2784:模板类映射的 reverse_iterator
【发布时间】:2016-07-22 05:18:54
【问题描述】:

我尝试创建如下迭代器基类:

template < typename object >
class IteratorBase : public IteratorInterface
{
protected:
    map<int, object> *objectMap;
    //typedef map<int, typename object> objectmap;
    typename map<int, typename object, less<int>, allocator<pair<const int, typename object>>>::reverse_iterator rit;

    //typedef typename objectmap::reverse_iterator riterator;
    //riterator rit;
    //typename objectmap::iterator it;
    typename map<int, typename object, less<int>, allocator<pair<const int, typename object>>>::iterator it;

    UINT32 limit;
    UINT32 start;
    UINT32 count;
    bool reverse;
public:
    ~IteratorBase()
    {
        objectMap = 0;
    }

    object *begin()
    {
        if(objectMap->size()==0)
            return 0;

        count = 0;

        int i=0;
        if(reverse)
        {
            for(rit = objectMap->rbegin(); rit!=objectMap->rend() && i<start; i++, rit++);

            if(rit == objectMap->rend())
                return 0;

            return &(rit->second);
        }
        else
        {
            for(it = objectMap->begin(); it!=objectMap->end() && i<start; i++, it++);

            if(it == objectMap->end())
                return 0;

            return &(it->second);
        }
    }

    object *next()
    {
        ++ count;
        if(limit)
            if(count>limit)
                return 0;

        if(reverse)
        {
            ++ rit;
            if(rit == objectMap->rend())
                return 0;

            return &rit->second;
        }
        else
        {
            ++ it;
            if(it == objectMap->end())
                return 0;

            return &it->second;
        }
    }


    object *getAt(int x, int *pos=0)
    {
        if(objectMap->size()==0)
            return 0;

        count = 0;

        int i=0;

        if(reverse)
        {
            map<int, object>::reverse_iterator itt;

            for(itt = objectMap->rbegin(); itt!=objectMap->rend() && i<start+x; i++, itt++);

            if(itt == objectMap->rend())
                return 0;

            if(pos)
                *pos = i;

            return &itt->second;
        }
        else
        {
            map<int, object>::iterator itt;

            for(itt = objectMap->begin(); itt!=objectMap->end() && i<start+x; i++, itt++);

            if(itt == objectMap->end())
                return 0;

            if(pos)
                *pos = i;

            return &itt->second;
        }
    }

};

但是有一个我不知道如何修复的错误:

1>c:\program files (x86)\microsoft visual studio 8\vc\include\xutility(1809) : error C2784: 'reverse_iterator<_RanIt>::difference_type std::operator -(const std::reverse_iterator<_RanIt> &,const std::reverse_iterator<_RanIt2> &)' : could not deduce template argument for 'const std::reverse_iterator<_RanIt> &' from 'const std::_Tree<_Traits>::iterator'
1>        with
1>        [
1>            _Traits=std::_Tmap_traits<int,TestDataItemEx,std::less<int>,std::allocator<std::pair<const int,TestDataItemEx>>,false>
1>        ]
1>        c:\program files (x86)\microsoft visual studio 8\vc\include\xutility(1856) : see declaration of 'std::operator -'
1>        c:\program files (x86)\microsoft visual studio 8\vc\include\xutility(1808) : while compiling class template member function 'std::reverse_iterator<_RanIt> std::reverse_iterator<_RanIt>::operator +(__w64 int) const'
1>        with
1>        [
1>            _RanIt=std::_Tree<std::_Tmap_traits<int,TestDataItemEx,std::less<int>,std::allocator<std::pair<const int,TestDataItemEx>>,false>>::iterator
1>        ]
1>        h:\test.140108\test\app1\Testapp.h(510) : see reference to class template instantiation 'std::reverse_iterator<_RanIt>' being compiled
1>        with
1>        [
1>            _RanIt=std::_Tree<std::_Tmap_traits<int,TestDataItemEx,std::less<int>,std::allocator<std::pair<const int,TestDataItemEx>>,false>>::iterator
1>        ]
1>        h:\test.140108\test\app1\Testapp.h(714) : see reference to class template instantiation 'IteratorBase<object>' being compiled
1>        with
1>        [
1>            object=TestDataItemEx
1>        ]

h:\test.140108\test\app1\Testapp.h(510):

typename map<int, typename object, less<int>, allocator<pair<const int, typename object>>>::reverse_iterator rit;

h:\test.140108\test\app1\Testapp.h(714):

class TestDataItemExIterator : public IteratorBase<struct TestDataItemEx>

.

1>c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(985): error C2676: binary '-=' : 'std::_Tree_iterator<_Mytree>' does not define this operator or a conversion to a type acceptable to the predefined operator
1>          with
1>          [
1>              _Mytree=std::_Tree_val<std::_Tree_simple_types<std::pair<const int,TestDataItemEx>>>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(984) : while compiling class template member function 'std::_Revranit<_RanIt,_Base> &std::_Revranit<_RanIt,_Base>::operator +=(__w64 int)'
1>          with
1>          [
1>              _RanIt=std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const int,TestDataItemEx>>>>,
1>              _Base=std::iterator<std::bidirectional_iterator_tag,std::pair<const int,TestDataItemEx>,__w64 int,std::pair<const int,TestDataItemEx> *,std::pair<const int,TestDataItemEx> &>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1194) : see reference to function template instantiation 'std::_Revranit<_RanIt,_Base> &std::_Revranit<_RanIt,_Base>::operator +=(__w64 int)' being compiled
1>          with
1>          [
1>              _RanIt=std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const int,TestDataItemEx>>>>,
1>              _Base=std::iterator<std::bidirectional_iterator_tag,std::pair<const int,TestDataItemEx>,__w64 int,std::pair<const int,TestDataItemEx> *,std::pair<const int,TestDataItemEx> &>
1>          ]
1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\xutility(1124) : see reference to class template instantiation 'std::_Revranit<_RanIt,_Base>' being compiled
1>          with
1>          [
1>              _RanIt=std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const int,TestDataItemEx>>>>,
1>              _Base=std::iterator<std::bidirectional_iterator_tag,std::pair<const int,TestDataItemEx>,__w64 int,std::pair<const int,TestDataItemEx> *,std::pair<const int,TestDataItemEx> &>
1>          ]
1>          h:\test.140108\test\app1\Testapp.h(510) : see reference to class template instantiation 'std::reverse_iterator<_RanIt>' being compiled
1>          with
1>          [
1>              _RanIt=std::_Tree_iterator<std::_Tree_val<std::_Tree_simple_types<std::pair<const int,TestDataItemEx>>>>
1>          ]
1>          h:\test.140108\test\app1\Testapp.h(714) : see reference to class template instantiation 'IteratorBase<object>' being compiled
1>          with
1>          [
1>              object=TestDataItemEx
1>          ]

请你帮帮我。

谢谢

【问题讨论】:

  • 代码试图计算两个反向迭代器之间的差异。这仅在基本迭代器是随机访问迭代器时才有效。地图没有那种迭代器。
  • 感谢 Bo,计算两个反向迭代器之间的差异不是我想要的。如何防止代码这样做?我的意思是,是否有任何方法可以对模板定义的任何结构对象使用 map 的反向迭代器而不会出现解析错误?

标签: c++ visual-studio-2012 visual-c++ stl visual-studio-2005


【解决方案1】:

我找到了。 这是 2005 和 2012 的 Microsoft Visual Studio 错误,不知道它是否会影响其他版本。 它向错误的地方报告编译错误。 真正的错误在于以下代码:

class TestDataItemExIterator : public IteratorBase<struct TestDataItemEx>
{
    ...
};

void main()
{   
    map<int, TestDataItemEx> objectMap;

    map<int, TestDataItemEx>::reverse_iterator it=objectMap.rbegin();

    if( (it+1) != objectMap.rend()) // Here is the error location.
    {
        int i=1;
    }
}

但是,错误报告显示了一个不同的地方,我花了 2 天时间才最终找到真正的问题所在。 :-)

【讨论】:

    【解决方案2】:

    VS2015 出现此问题。
    解决方法如下:

    更改自:

    *(c1.end()-1)
    

    到:

    it = c1.end();
    it--;
    

    【讨论】:

      猜你喜欢
      • 2014-06-08
      • 1970-01-01
      • 1970-01-01
      • 2017-03-23
      • 1970-01-01
      • 2012-01-14
      • 2022-01-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多