mem_fun error

Discussion in 'C++' started by joseph cook, Mar 7, 2007.

  1. joseph  cook

    joseph cook Guest

    I am getting a compile error on any compiler I try, so I know I have
    an error here. Can anyone see it?

    //includes

    class Foo
    {
    public:
    Foo(int a){m_hi = a;}
    int hi(){return m_hi;}

    int m_hi;
    };

    int main()
    {
    std::vector<Foo *> data;
    data.push_back(new Foo(3));

    max_element(data.begin(),
    data.end(),
    std::mem_fun(&Foo::hi));

    }

    gives a compile error:
    in gcc: stl_algo.h:4565: error: no match for call to
    '(std::mem_fun_t<int, Foo>) (Foo*&,Foo*&)'
    stl_function.h:600: note: candidates are: _Ret
    std::mem_fun_t<_Ret,_Tp>::eek:perator()(_Tp*) const [with _Ret = int,
    _Tp = Foo]

    help!

    }
     
    joseph cook, Mar 7, 2007
    #1
    1. Advertising

  2. joseph  cook

    Ian Collins Guest

    joseph cook wrote:
    > I am getting a compile error on any compiler I try, so I know I have
    > an error here. Can anyone see it?
    >
    > //includes
    >

    There aren't any....

    --
    Ian Collins.
     
    Ian Collins, Mar 7, 2007
    #2
    1. Advertising

  3. joseph  cook

    joseph cook Guest


    > > //includes

    >
    > There aren't any....
    >


    I meant "all neccessary includes are being included".

    To be pendantic about it:
    #include <functional>
    #include <algorithm>
    #include <vector>

    are at the top of my file
     
    joseph cook, Mar 7, 2007
    #3
  4. joseph  cook

    mlimber Guest

    On Mar 7, 4:56 pm, "joseph cook" <> wrote:
    > I am getting a compile error on any compiler I try, so I know I have
    > an error here. Can anyone see it?
    >
    > //includes
    >
    > class Foo
    > {
    > public:
    > Foo(int a){m_hi = a;}
    > int hi(){return m_hi;}
    >
    > int m_hi;
    >
    > };
    >
    > int main()
    > {
    > std::vector<Foo *> data;
    > data.push_back(new Foo(3));
    >
    > max_element(data.begin(),
    > data.end(),
    > std::mem_fun(&Foo::hi));
    >
    > }
    >
    > gives a compile error:
    > in gcc: stl_algo.h:4565: error: no match for call to
    > '(std::mem_fun_t<int, Foo>) (Foo*&,Foo*&)'
    > stl_function.h:600: note: candidates are: _Ret
    > std::mem_fun_t<_Ret,_Tp>::eek:perator()(_Tp*) const [with _Ret = int,
    > _Tp = Foo]
    >
    > help!
    >
    > }


    You're specifying a member function adapter where you need a
    comparator. A member function is invoked like:

    some_mem_fun( *i )

    where some_mem_fun is the functor resulting from your mem_fun call and
    i is an iterator in the range [begin, end). Note that the functor only
    takes one parameter. A comparator, on the other hand, has a signature
    like:

    bool less_than( a, b )

    Probably what you intended is something like this:

    for_each( data.begin(),
    data.end(),
    mem_fun(&Foo::hi));

    or

    int mx = numeric_limits<int>::min();
    for( vector<Foo*>::const_iterator i=data.begin();
    i != data.end(); ++i )
    {
    mx = max( (*i)->hi(), mx );
    }

    (Of course, this would require Foo::hi() to be a const function.)

    Cheers! --M
     
    mlimber, Mar 7, 2007
    #4
  5. joseph  cook

    Sarath Guest

    On Mar 8, 6:56 am, "joseph cook" <> wrote:
    > I am getting a compile error on any compiler I try, so I know I have
    > an error here. Can anyone see it?
    >
    > //includes
    >
    > class Foo
    > {
    > public:
    > Foo(int a){m_hi = a;}
    > int hi(){return m_hi;}
    >
    > int m_hi;
    >
    > };
    >
    > int main()
    > {
    > std::vector<Foo *> data;
    > data.push_back(new Foo(3));
    >
    > max_element(data.begin(),
    > data.end(),
    > std::mem_fun(&Foo::hi));
    >
    > }
    >
    > gives a compile error:
    > in gcc: stl_algo.h:4565: error: no match for call to
    > '(std::mem_fun_t<int, Foo>) (Foo*&,Foo*&)'
    > stl_function.h:600: note: candidates are: _Ret
    > std::mem_fun_t<_Ret,_Tp>::eek:perator()(_Tp*) const [with _Ret = int,
    > _Tp = Foo]
    >
    > help!
    >
    >
    >
    > }- Hide quoted text -
    >
    > - Show quoted text -


    Foo::hi should be either static or Global which takes two arguments as
    input and bool as return. now it's in the context of a class (object)
     
    Sarath, Mar 8, 2007
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. k0tic
    Replies:
    1
    Views:
    584
    Alf P. Steinbach
    Sep 4, 2003
  2. Maitre Bart

    STL custom mem_fun

    Maitre Bart, Feb 10, 2004, in forum: C++
    Replies:
    2
    Views:
    603
    Maitre Bart
    Feb 11, 2004
  3. Old Wolf

    mem_fun with template function

    Old Wolf, Aug 16, 2004, in forum: C++
    Replies:
    5
    Views:
    703
    Daniel T.
    Aug 17, 2004
  4. Robbie Hatley

    Problem using mem_fun.

    Robbie Hatley, Oct 24, 2004, in forum: C++
    Replies:
    2
    Views:
    1,211
    Robbie Hatley
    Oct 25, 2004
  5. joseph  cook

    mem_fun error

    joseph cook, Mar 7, 2007, in forum: C++
    Replies:
    2
    Views:
    403
    joseph cook
    Mar 7, 2007
Loading...

Share This Page