Re: mem_fun_ref with 1 argument?

Discussion in 'C++' started by Raoul Gough, Aug 26, 2003.

  1. Raoul Gough

    Raoul Gough Guest

    "Jimmy Johns" <> writes:

    > Hi, I have a class defined like so:
    >
    > class foo {
    > public:
    > typedef std::pair<double, double> ret_type;
    > ret_type func1(double n) const // notice the const
    > {
    > // do soemthing
    > // return ret_type
    > }
    > };
    >
    > now in a function:
    >
    > std::vector<double> v1;
    > //put some stuff in v1;
    > std::vector<foo::ret_type> res;
    > std::transform(v1.begin(), v1.end(),
    > std::back_inserter(res),
    > std::mem_fun_ref(&foo::func1) // this line gives me problems.
    > };


    But func1 is a member function, so it needs an object to operate
    on. IIRC, mem_fun_ref converts a member function into a function that
    takes that object type as its first parameter. You still need to pass
    it a foo object, though. Something like

    std::bind1st (std::mem_fun_ref (&foo::func1), foo_object);

    Should create a unary function that takes a double.

    > okay, so I checked SGI's site on this, and there seems to be a mem_fun1_ref
    > function that allows for a single argument, but that eventually resulted in
    > the same error. I've also tried using bind2nd(mem_fun_ref(&foo::func1),
    > *cit++), where cit is an iterator initially pointing to v1.begin(), but that
    > gives an error as well. The error message is in algorithm header and the
    > message is "term does not evaluate to a function". So now I'm pretty
    > stumped. What is the proper way to do this??


    Using an expression with a side-effect almost certainly won't do what
    you want it to (it will only get evaluated once, before being passed
    to bind2nd which returns an object containing a fixed value).

    --
    Raoul Gough
    "Let there be one measure for wine throughout our kingdom, and one
    measure for ale, and one measure for corn" - Magna Carta
     
    Raoul Gough, Aug 26, 2003
    #1
    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. Anders

    mem_fun_ref

    Anders, Jun 30, 2003, in forum: C++
    Replies:
    2
    Views:
    1,692
    tom_usenet
    Jul 1, 2003
  2. Jan Rendek
    Replies:
    0
    Views:
    501
    Jan Rendek
    Jul 15, 2003
  3. Michiel Salters

    Re: How to use bind2nd with mem_fun_ref?

    Michiel Salters, Jul 16, 2003, in forum: C++
    Replies:
    0
    Views:
    414
    Michiel Salters
    Jul 16, 2003
  4. Ahti Legonkov

    Re: mem_fun_ref with 1 argument?

    Ahti Legonkov, Aug 26, 2003, in forum: C++
    Replies:
    0
    Views:
    484
    Ahti Legonkov
    Aug 26, 2003
  5. Ioannis Vranos

    mem_fun vs mem_fun_ref

    Ioannis Vranos, Mar 17, 2005, in forum: C++
    Replies:
    13
    Views:
    7,028
    Pete Becker
    Mar 17, 2005
Loading...

Share This Page