gpt4 book ai didi

c++ - VC2012错误C2676,VC2005错误C2784 : reverse_iterator for map of template classes

转载 作者:行者123 更新时间:2023-11-28 05:45:08 25 4
gpt4 key购买 nike

我尝试创建一个迭代器基类,如下所示:

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> ]

你能帮帮我吗

谢谢

最佳答案

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

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 天时间才终于找到真正的问题所在。 :-)

关于c++ - VC2012错误C2676,VC2005错误C2784 : reverse_iterator for map of template classes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36372613/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com