By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,820 Members | 1,189 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,820 IT Pros & Developers. It's quick & easy.

for_each bind2nd reference to reference error

P: n/a
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<Cv(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?
Mar 9 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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<Cv(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.
Mar 9 '07 #2

P: n/a
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<Cv(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)));
}
Mar 9 '07 #3

P: n/a
On Mar 9, 6:09 pm, Chris Roth <czr...@mail.usask.cawrote:
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<Cv(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 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.