reverse_iterator
-
I have an STL list, and I want to move on it from back to front. I am trying to use the reverse_iterator, but I am getting a compilation error. Here is my code:
typedef std::list<RECT> RectList;
RectList myList; // The list is created and initialised elsewherefor ( RectList::reverse_iterator ri = myList.rbegin();
ri != RectList.rend(); ++ri )
{
*ri = *(ri + 1);
}I get the following errors in the last line:
error C2784: 'class std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> __cdecl std::operator +(_D,const class std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> &)' : could not deduce template argument for '' from 'class std::reverse_bidirectional_iterator<class std::list<struct tagRECT,class std::allocator<struct tagRECT> >::iterator,struct tagRECT,struct tagRECT &,struct tagRECT *,int>'
error C2676: binary '+' : 'class std::reverse_bidirectional_iterator<class std::list<struct tagRECT,class std::allocator<struct tagRECT> >::iterator,struct tagRECT,struct tagRECT &,struct tagRECT *,int>' does not define this operator or a conversion to a type acceptable to the predefined operator
Hosam Aly Mahmoud
-
I have an STL list, and I want to move on it from back to front. I am trying to use the reverse_iterator, but I am getting a compilation error. Here is my code:
typedef std::list<RECT> RectList;
RectList myList; // The list is created and initialised elsewherefor ( RectList::reverse_iterator ri = myList.rbegin();
ri != RectList.rend(); ++ri )
{
*ri = *(ri + 1);
}I get the following errors in the last line:
error C2784: 'class std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> __cdecl std::operator +(_D,const class std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> &)' : could not deduce template argument for '' from 'class std::reverse_bidirectional_iterator<class std::list<struct tagRECT,class std::allocator<struct tagRECT> >::iterator,struct tagRECT,struct tagRECT &,struct tagRECT *,int>'
error C2676: binary '+' : 'class std::reverse_bidirectional_iterator<class std::list<struct tagRECT,class std::allocator<struct tagRECT> >::iterator,struct tagRECT,struct tagRECT &,struct tagRECT *,int>' does not define this operator or a conversion to a type acceptable to the predefined operator
Hosam Aly Mahmoud
You can only increment or decrement iterators, not add a numeric offset to them.
"Sucks less" isn't progress - Kent Beck [^] Awasu 1.1.2 [^]: A free RSS reader with support for Code Project.
-
You can only increment or decrement iterators, not add a numeric offset to them.
"Sucks less" isn't progress - Kent Beck [^] Awasu 1.1.2 [^]: A free RSS reader with support for Code Project.
But he can dereference them and use operators on the referenced object. Provided that the operators are defined for the object. Was it that what he tried: *r+1 ?
Who is 'General Failure'? And why is he reading my harddisk?!?
-
But he can dereference them and use operators on the referenced object. Provided that the operators are defined for the object. Was it that what he tried: *r+1 ?
Who is 'General Failure'? And why is he reading my harddisk?!?
His code is doing this:
*ri = *(ri + 1);
i.e. he is trying to add one to the iterator, *then* dereferencing it.
"Sucks less" isn't progress - Kent Beck [^] Awasu 1.1.2 [^]: A free RSS reader with support for Code Project.
-
I have an STL list, and I want to move on it from back to front. I am trying to use the reverse_iterator, but I am getting a compilation error. Here is my code:
typedef std::list<RECT> RectList;
RectList myList; // The list is created and initialised elsewherefor ( RectList::reverse_iterator ri = myList.rbegin();
ri != RectList.rend(); ++ri )
{
*ri = *(ri + 1);
}I get the following errors in the last line:
error C2784: 'class std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> __cdecl std::operator +(_D,const class std::reverse_iterator<_RI,_Ty,_Rt,_Pt,_D> &)' : could not deduce template argument for '' from 'class std::reverse_bidirectional_iterator<class std::list<struct tagRECT,class std::allocator<struct tagRECT> >::iterator,struct tagRECT,struct tagRECT &,struct tagRECT *,int>'
error C2676: binary '+' : 'class std::reverse_bidirectional_iterator<class std::list<struct tagRECT,class std::allocator<struct tagRECT> >::iterator,struct tagRECT,struct tagRECT &,struct tagRECT *,int>' does not define this operator or a conversion to a type acceptable to the predefined operator
Hosam Aly Mahmoud
You get an error here, because you can not use '+' with an iterator. If you want to assign the value of the next object to the current one, I would do:
{
RectList::reverse_iterator next = ri++;
*ri = *next;
}If you want to access an iterator that is more than one step away, you can use
std::advance()
to step the iterator many steps. If you intended to add a value to the value referenced byri
, you need to write:*ri = *ri + value;
Who is 'General Failure'? And why is he reading my harddisk?!?
-
You can only increment or decrement iterators, not add a numeric offset to them.
"Sucks less" isn't progress - Kent Beck [^] Awasu 1.1.2 [^]: A free RSS reader with support for Code Project.
Thanks for your reply. But why can't I add to the iterator? According to MSDN[^], the reverse iterator contains operator+. What I don't know is what this operator takes as input. Anyway, I changed my code to this and it compiled:
for ( RectList::reverse_iterator ri = myList.rbegin(), ri2 = myList.rbegin();
ri2 != myList.rend(); ++ri )
*ri = *(++ri2);Thank you for your help.
Hosam Aly Mahmoud
-
You get an error here, because you can not use '+' with an iterator. If you want to assign the value of the next object to the current one, I would do:
{
RectList::reverse_iterator next = ri++;
*ri = *next;
}If you want to access an iterator that is more than one step away, you can use
std::advance()
to step the iterator many steps. If you intended to add a value to the value referenced byri
, you need to write:*ri = *ri + value;
Who is 'General Failure'? And why is he reading my harddisk?!?
Thanks for your reply. Your code does the opposite of what I wanted. I would change it to:
{
RectList::reverse_iterator next = ri;
*ri = *(++next);
}Thanks anyway.
Hosam Aly Mahmoud