Go Back   Velocity Reviews > Newsgroups > C++
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read


Reply

C++ - for_each bind2nd reference to reference error

 
Thread Tools Search this Thread
Old 03-09-2007, 10:09 PM   #1
Chris Roth
 
Posts: n/a
Default for_each bind2nd reference to reference error

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?
  Reply With Quote
Old 03-09-2007, 10:22 PM   #2
Noah Roberts
 
Posts: n/a
Default Re: for_each bind2nd reference to reference error

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.
  Reply With Quote
Old 03-09-2007, 10:31 PM   #3
Piyo
 
Posts: n/a
Default Re: for_each bind2nd reference to reference error

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)));
}
  Reply With Quote
Old 03-09-2007, 11:47 PM   #4
AnonMail2005@gmail.com
 
Posts: n/a
Default Re: for_each bind2nd reference to reference error

On Mar 9, 6:09 pm, Chris Roth <czr...@mail.usask.ca> 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));

  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help on Modelsim VHDL syntax? ASAP:) kaji General Help Related Topics 0 03-14-2007 09:43 PM
Need help on a Modelsim VHDL Syntax? ASAP:) kaji Software 0 03-14-2007 09:43 PM
Need Help on a Modelsim VHDL Syntax....ASAP:) kaji Hardware 0 03-14-2007 09:41 PM
Parser Error Message: Could not load type 'Microsoft.SharePoint.ApplicationPages.Glob rasmita General Help Related Topics 0 09-05-2006 04:49 AM
Parser Error Message: Could not load type 'Microsoft.SharePoint.ApplicationPages.Glob rasmita General Help Related Topics 0 09-05-2006 04:46 AM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47