j'ai essayé de créer une classe de base iterator comme suit:Erreur C2676 dans VC2012 et C2784 à VC2005: reverse_iterator pour carte des classes de modèle
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;
}
}
};
Mais il y a une erreur que je ne sais pas comment résoudre :
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 \ Testa pp.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> ]
Pourriez-vous m'aider s'il vous plaît.
Merci
Quelque part le code essaie de calculer la différence entre deux itérateurs à rebours. Cela ne fonctionne que si l'itérateur de base est un itérateur à accès aléatoire. Une carte n'a pas ce type d'itérateur. –
Merci Bo, calculer la différence entre deux itérateurs inverse n'est pas ce que je veux. Comment empêcher le code de le faire? Je veux dire, y a-t-il une méthode qui peut utiliser l'itérateur inverse pour la carte pour tout objet struct défini par template sans erreur d'analyse? –