我尝试创建一个迭代器基类,如下所示:
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> ]
你能帮帮我吗?
谢谢
答案 0 :(得分: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天才找到真正的问题所在。 : - )
答案 1 :(得分:0)
此问题发生在VS2015中 解决了它如下:
改为:
*(c1.end()-1)
为:
it = c1.end();
it--;