Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. reverse_iterator

reverse_iterator

Scheduled Pinned Locked Moved C / C++ / MFC
c++help
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Offline
    H Offline
    Hosam Aly Mahmoud
    wrote on last edited by
    #1

    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 elsewhere

    for ( 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

    T J 2 Replies Last reply
    0
    • H 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 elsewhere

      for ( 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

      T Offline
      T Offline
      Taka Muraoka
      wrote on last edited by
      #2

      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.

      J H 2 Replies Last reply
      0
      • T Taka Muraoka

        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.

        J Offline
        J Offline
        jhwurmbach
        wrote on last edited by
        #3

        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?!?

        T 1 Reply Last reply
        0
        • J jhwurmbach

          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?!?

          T Offline
          T Offline
          Taka Muraoka
          wrote on last edited by
          #4

          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.

          1 Reply Last reply
          0
          • H 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 elsewhere

            for ( 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

            J Offline
            J Offline
            jhwurmbach
            wrote on last edited by
            #5

            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 by ri, you need to write:

            *ri = *ri + value;


            Who is 'General Failure'? And why is he reading my harddisk?!?

            H 1 Reply Last reply
            0
            • T Taka Muraoka

              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.

              H Offline
              H Offline
              Hosam Aly Mahmoud
              wrote on last edited by
              #6

              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

              1 Reply Last reply
              0
              • J jhwurmbach

                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 by ri, you need to write:

                *ri = *ri + value;


                Who is 'General Failure'? And why is he reading my harddisk?!?

                H Offline
                H Offline
                Hosam Aly Mahmoud
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups