473,325 Members | 2,608 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

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<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
3 2355
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
by: John Black | last post by:
Hi, I am not familiar with for_each very well, suppoase I have a vector<pair<unsigned int, unsigned int> > vec1 and the contents are {<0x00000000, 0x000000FF>, <0x10000000, 0x2FFFFFFF>} what...
1
by: trash | last post by:
I do this kindda stuff often: class CVisitableObjectVector : public std::vector<Loki::BaseVisitable<>*> { void Enable(bool enable); .... };
11
by: franklini | last post by:
hello people, just wanted to say thanks again for the help in the past. i have a new problem which am wondering if any body can help me with. i have written this abtract class shape and its...
1
by: Capstar | last post by:
Hi NG, I have a question on std::for_each. I try to use this in combination with std::bind2nd to call a method of all functions in a container (std::set or std::map) and pass that method the...
1
by: ibe | last post by:
Hi, i can't get the clue: I hav a loop in my code like this: ---snip--- class Foo { accept(Visitor& v) { v.visit(*this); }
2
by: waitan | last post by:
#include <algorithm> #include <iostream> #include <fstream> #include <string> #include <vector> #include <sstream> #include <iterator> #include <iomanip> using namespace std;
9
by: Chris Roth | last post by:
I have a vector of vectors: vector< vector<double v; and have initialized it with: v( 5 ); as I know I will have 5 columns of data. At this point, I read text file data into each of the the...
2
by: benben | last post by:
I wrote the following lines: #include <locale> #include <functional> #include <algorithm> int main() { using namespace std; bind2nd(ptr_fun(tolower<char>), locale());
9
by: nguillot | last post by:
Hello I used to loop on a std::map<k, dto act on the data (d) like that (d being a class with setIntMember method): typedef std::map<k, dtMap; struct setIntMember { setIntMember(int j) :...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.