std::find, std::list::remove/erase

This is a discussion on std::find, std::list::remove/erase within the C++ Programming forums, part of the General Programming Boards category; Edit: Solved. I was using std::find incorrectly. Hi, quick question. How do you get operator== working with std::find? I got ...

  1. #1
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034

    Question std::find, std::list::remove/erase

    Edit: Solved. I was using std::find incorrectly.

    Hi, quick question. How do you get operator== working with std::find? I got it working for std::list::remove, but using std::find to grab the iterator to use for std::list::erase fails.

    Code:
    error: no match for ‘operator==’ in ‘__first.std::_List_iterator<_Tp>::operator* [with _Tp = klass, _Tp& = klass&]() == __val’
    
    note: candidates are: bool klass::operator==(klass&)
    note:                 bool klass::operator==(const klass&)
    note:                 bool klass::operator==(klass&) const
    note:                 bool klass::operator==(const klass&) const
    stl_algo.h
    Code:
      /// This is an overload used by find() for the Input Iterator case.
      template<typename _InputIterator, typename _Tp>
        inline _InputIterator
        __find(_InputIterator __first, _InputIterator __last,
    	   const _Tp& __val, input_iterator_tag)
        {
          while (__first != __last && !(*__first == __val)) // error here
    	++__first;
          return __first;
        }
    So __val is klass&, and *__first is.. iterator dereferenced into klass&?

    What's the problem then?

    Thanks!
    Last edited by Dae; 09-24-2009 at 01:09 AM.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,772
    You should show your code, not the code from the standard library. It looks like you did not declare your member operator== as a const member function.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by laserlight View Post
    You should show your code, not the code from the standard library. It looks like you did not declare your member operator== as a const member function.
    Look at the candidates.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,772
    Quote Originally Posted by Dae
    Look at the candidates.
    Right. Perhaps I should have asked instead: why are there so many versions of member operator==?

    Good to see that you solved it yourself, but you really should have posted your own code. I am not interested in what the standard library implementation is because it is far less likely wrong than your code.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    Quote Originally Posted by laserlight View Post
    Right. Perhaps I should have asked instead: why are there so many versions of member operator==?
    I wanted to make sure it wasn't a calling compatibility issue, and those are the 4 ways to define an equality between the exact same type. I've since removed 3 of them.

    I know you're not interested in the STL. I threw it in there for easy access and reference. Showing you that code might trigger a case when one of you had this issue. I didn't have to post the operator== code because the interface is already shown in the error. It didn't occur to me to show the std::find code, but now I know.

    Take it easy
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. std::string::find vs std::find
    By petry in forum C++ Programming
    Replies: 17
    Last Post: 07-08-2009, 05:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21