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. operator != for iterators

operator != for iterators

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++tutorialquestion
5 Posts 2 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.
  • M Offline
    M Offline
    mcoloney
    wrote on last edited by
    #1

    Friends, I'm trying to create a function so I can print the conents of a STL list from main(). It looks like: void TraverseAndDisplay(list< PhoneRecord>::iterator& itor, const list< PhoneRecord>& l) { while (itor != l.end()) { cout << *itor++ << " "; } cout << endl; } Unfortunately, because PhoneRecord is not a primitive data type (it's my own class), the default operator != won't work. The exact error I'm getting is: error C2679: binary '!=' : no operator defined which takes a right-hand operand of type 'class std::list<class PhoneRecord,class std::allocator<class PhoneRecord> >::const_iterator' (or there is no acceptable conversion) Obviously I need to overload the != to work. I don't know how to overload the != operator to work for iterators in this case? Thanks in advance for your help!

    D 1 Reply Last reply
    0
    • M mcoloney

      Friends, I'm trying to create a function so I can print the conents of a STL list from main(). It looks like: void TraverseAndDisplay(list< PhoneRecord>::iterator& itor, const list< PhoneRecord>& l) { while (itor != l.end()) { cout << *itor++ << " "; } cout << endl; } Unfortunately, because PhoneRecord is not a primitive data type (it's my own class), the default operator != won't work. The exact error I'm getting is: error C2679: binary '!=' : no operator defined which takes a right-hand operand of type 'class std::list<class PhoneRecord,class std::allocator<class PhoneRecord> >::const_iterator' (or there is no acceptable conversion) Obviously I need to overload the != to work. I don't know how to overload the != operator to work for iterators in this case? Thanks in advance for your help!

      D Offline
      D Offline
      Dave Bryant
      wrote on last edited by
      #2

      Can you please repost the code, using < and > for the angle brackets so that we can see the template parameters? Dave http://www.cloudsofheaven.org

      M 1 Reply Last reply
      0
      • D Dave Bryant

        Can you please repost the code, using < and > for the angle brackets so that we can see the template parameters? Dave http://www.cloudsofheaven.org

        M Offline
        M Offline
        mcoloney
        wrote on last edited by
        #3

        Sorry about that. It's reposted. Ignore the space after the < and before PhoneRecord.. it was making it a smiley.

        D 1 Reply Last reply
        0
        • M mcoloney

          Sorry about that. It's reposted. Ignore the space after the < and before PhoneRecord.. it was making it a smiley.

          D Offline
          D Offline
          Dave Bryant
          wrote on last edited by
          #4

          I suspect it is because you are comparing an iterator with a const_iterator. Since the list passed in as a parameter is constant, it will only return a const_iterator, so you could either make the list non-cost, or pass in a const_iterator instead of an iterator as the first parameter (the better option as you do not appear to be modifying the list here). Alternatively, you could pass in the end iterator as a parameter rather than passing in the entire list - this is the approach used by STL, as then the type of container can be changed without modifying the function itself. Dave http://www.cloudsofheaven.org

          M 1 Reply Last reply
          0
          • D Dave Bryant

            I suspect it is because you are comparing an iterator with a const_iterator. Since the list passed in as a parameter is constant, it will only return a const_iterator, so you could either make the list non-cost, or pass in a const_iterator instead of an iterator as the first parameter (the better option as you do not appear to be modifying the list here). Alternatively, you could pass in the end iterator as a parameter rather than passing in the entire list - this is the approach used by STL, as then the type of container can be changed without modifying the function itself. Dave http://www.cloudsofheaven.org

            M Offline
            M Offline
            mcoloney
            wrote on last edited by
            #5

            Dave, thank you so much for your help!

            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