Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!weretis.net!feeder1.news.weretis.net!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Fencer Newsgroups: comp.lang.c++ Subject: Problem with functor, maybe I need operator== for my struct? Date: Mon, 25 Jul 2011 13:25:58 +0200 Lines: 62 Message-ID: <99525sF1frU1@mid.individual.net> Mime-Version: 1.0 Content-Type: text/plain; charset=windows-1252; format=flowed Content-Transfer-Encoding: 8bit X-Trace: individual.net kULZgCpcmH/TpEpgMdLEJAbuGiPQWTuDDd/faaYmsg8QmvWN21 Cancel-Lock: sha1:WHI0fNR+x4JqhYzMU+qiQjT1Tsc= User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:5.0) Gecko/20110624 Thunderbird/5.0 Xref: x330-a1.tempe.blueboxinc.net comp.lang.c++:8189 Hello, I am very rusty in C++ and I'm having some problems: I have a std::set and I need to look through the set to find if any of the instances of SomeClass that it contains has a particular value for one of its data members. I know I could simply iterate through the set myself and check every SomeClass instance if any of them is a match, but I tried to do it using functors instead. The following simplified test program does not build, and it seems to be that operator== is missing for the struct s1. Could you gurus help me explain why my test program does not build and how I should fix it? #include #include #include using namespace std; struct s1 { s1(int u, int v) : u(u), v(v) {} int u, v; }; struct OnV : public unary_function { explicit OnV(int v) : v(v) {} bool operator()(s1 const& inst) const { return v == inst.v; } int v; }; int main() { set a_set; find(a_set.begin(), a_set.end(), OnV(4711)); } $ g++ -Wall -Wextra -std=c++98 -pedantic functor.cpp -o functor.exe /usr/lib/gcc/i686-pc-cygwin/4.3.4/include/c++/bits/stl_algo.h: In function ‘_InputIterator std::__find(_InputIterator, _InputIterator, const _Tp&, std::input_iterator_tag) [with _InputIterator = std::_Rb_tree_const_iterator, _Tp = OnV]’: /usr/lib/gcc/i686-pc-cygwin/4.3.4/include/c++/bits/stl_algo.h:3814: instantiated from ‘_IIter std::find(_IIter, _IIter, const _Tp&) [with _IIter = std::_Rb_tree_const_iterator, _Tp = OnV]’ functor.cpp:30: instantiated from here /usr/lib/gcc/i686-pc-cygwin/4.3.4/include/c++/bits/stl_algo.h:151: error: no match for ‘operator==’ in ‘__first.std::_Rb_tree_const_iterator<_Tp>::operator* [with _Tp = s1]() == __val’ Thank you for reading and thanks for any help! Even though I could use the alternative solution I mentioned above that does not involve functors, I wanted to know what I did wrong with my functor and hopefully learn something from your replies. - Fencer