for_each bind2nd reference to reference error

Discussion in 'C++' started by Chris Roth, Mar 9, 2007.

  1. Chris Roth

    Chris Roth Guest

    I have a vector (v) containing objects of class C.

    class C
    {
    private:
    double d;
    public:
    void foo( B& b );
    };

    class B
    {
    public:
    int i;
    double x;
    // lots of other things, which is why its passed by reference.
    };

    I'd like to run function foo for each element in v. foo takes one
    argument of class B passed by reference. The following lines produces a
    reference to reference error:

    vector<C> v(50);

    B b;

    for_each( v.begin(), v.end(),
    bind2nd( mem_fun_ref(&C::foo), b ) );

    How can I get around this using for_each?
     
    Chris Roth, Mar 9, 2007
    #1
    1. Advertising

  2. Chris Roth

    Noah Roberts Guest

    Chris Roth wrote:
    > I have a vector (v) containing objects of class C.
    >
    > class C
    > {
    > private:
    > double d;
    > public:
    > void foo( B& b );
    > };
    >
    > class B
    > {
    > public:
    > int i;
    > double x;
    > // lots of other things, which is why its passed by reference.
    > };
    >
    > I'd like to run function foo for each element in v. foo takes one
    > argument of class B passed by reference. The following lines produces a
    > reference to reference error:
    >
    > vector<C> v(50);
    >
    > B b;
    >
    > for_each( v.begin(), v.end(),
    > bind2nd( mem_fun_ref(&C::foo), b ) );
    >
    > How can I get around this using for_each?


    Not with bind2nd and friends. You need boost::bind or tr1::bind. A
    much more general and powerful solution anyway. You also get to forget
    all about bind1st/2nd, mem_fun, mem_fun_ref, etc...

    Without boost::bind and boost::ref you're going to be stuck making your
    own functor or a for loop.
     
    Noah Roberts, Mar 9, 2007
    #2
    1. Advertising

  3. Chris Roth

    Piyo Guest

    Noah Roberts wrote:
    >
    > Not with bind2nd and friends. You need boost::bind or tr1::bind. A
    > much more general and powerful solution anyway. You also get to forget
    > all about bind1st/2nd, mem_fun, mem_fun_ref, etc...
    >
    > Without boost::bind and boost::ref you're going to be stuck making your
    > own functor or a for loop.


    Yep, I gave up and went boost with this also:


    #include <vector>
    #include <algorithm>
    #include <boost/bind.hpp>

    using namespace std;
    using namespace boost;

    class B
    {
    public:
    int i;
    double x;
    };


    class C
    {
    private:
    double d;
    public:
    void foo( B& b ){}
    };


    int
    main()
    {
    vector<C> v(50);

    B b;
    //for_each( v.begin(), v.end(), mem_fun_ref(&C::foo), b ) );
    for_each( v.begin(), v.end(),
    bind(&C::foo, _1, ref(b)));
    }
     
    Piyo, Mar 9, 2007
    #3
  4. Chris Roth

    Guest

    On Mar 9, 6:09 pm, Chris Roth <> wrote:
    > I have a vector (v) containing objects of class C.
    >
    > class C
    > {
    > private:
    > double d;
    > public:
    > void foo( B& b );
    >
    > };
    >
    > class B
    > {
    > public:
    > int i;
    > double x;
    > // lots of other things, which is why its passed by reference.
    >
    > };
    >
    > I'd like to run function foo for each element in v. foo takes one
    > argument of class B passed by reference. The following lines produces a
    > reference to reference error:
    >
    > vector<C> v(50);
    >
    > B b;
    >
    > for_each( v.begin(), v.end(),
    > bind2nd( mem_fun_ref(&C::foo), b ) );
    >
    > How can I get around this using for_each?


    class MyFunctor
    {
    public:
    MyFunctor (B & b) : m_b (b)
    {}

    void operator () (C & c)
    {
    c.foo (m_b);
    }

    private:
    B & m_b;
    }

    std::for_each (v.begin (), v.end (), MyFunctor (b));
     
    , Mar 10, 2007
    #4
    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. Jan Rendek
    Replies:
    0
    Views:
    511
    Jan Rendek
    Jul 15, 2003
  2. Michiel Salters

    Re: How to use bind2nd with mem_fun_ref?

    Michiel Salters, Jul 16, 2003, in forum: C++
    Replies:
    0
    Views:
    423
    Michiel Salters
    Jul 16, 2003
  3. red floyd
    Replies:
    1
    Views:
    1,930
    Jeff Schwab
    Dec 29, 2003
  4. John Black
    Replies:
    3
    Views:
    534
    Dave Moore
    Jun 2, 2004
  5. cpp
    Replies:
    4
    Views:
    994
    Stuart Redmann
    Feb 22, 2011
Loading...

Share This Page