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

  1. #1
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    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