boost::bind problem in VC8

I

IndyStef

I am trying to use boost's bind on a member function, on the VC8
compiler.
After using several different attempts, I could not get it to work.
Does
anybody know what is wrong with the code below? The function that
doesn't
compile is foo::DoTheStuff. All three variations of the for-each loop
won't
build.

Thank you,
Stefan

// Boost_Bind.cpp : Defines the entry point for the console
application.
//

#include "stdafx.h"
#include <boost/bind.hpp>
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>

using namespace std;

class foo
{
public:
foo() {};
virtual ~foo() {};

void DoTheStuff();
void PrintInt( int a_nI );

protected:
vector<int> m_vecInt;
};

void foo::printInt(int a_nI )
{
cout << a_nI << endl;
}

void foo::DoTheStuff()
{
m_vecInt.reserve(50);
generate(m_vecInt.begin(),m_vecInt.end(),rand);

for_each(m_vecInt.begin(),m_vecInt.end(),boost::bind(&foo::printInt,
_1));

//for_each(m_vecInt.begin(),m_vecInt.end(),boost::bind<void>(mem_fun(&foo::printInt),
_1));
//for_each(m_vecInt.begin(),m_vecInt.end(),mem_fun(&foo::printInt));
}

int _tmain(int argc, _TCHAR* argv[])
{
foo a;
a.DoTheStuff();
return 0;
}
 
M

mlimber

IndyStef said:
I am trying to use boost's bind on a member function, on the VC8
compiler.
After using several different attempts, I could not get it to work.
Does
anybody know what is wrong with the code below? The function that
doesn't
compile is foo::DoTheStuff. All three variations of the for-each loop
won't
build.

Thank you,
Stefan

// Boost_Bind.cpp : Defines the entry point for the console
application.
//

#include "stdafx.h"
#include <boost/bind.hpp>
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>

using namespace std;

class foo
{
public:
foo() {};
virtual ~foo() {};

void DoTheStuff();
void PrintInt( int a_nI );

protected:
vector<int> m_vecInt;
};

void foo::printInt(int a_nI )
{
cout << a_nI << endl;
}

void foo::DoTheStuff()
{
m_vecInt.reserve(50);
generate(m_vecInt.begin(),m_vecInt.end(),rand);

for_each(m_vecInt.begin(),m_vecInt.end(),boost::bind(&foo::printInt,
_1));

//for_each(m_vecInt.begin(),m_vecInt.end(),boost::bind<void>(mem_fun(&foo::printInt),
_1));
//for_each(m_vecInt.begin(),m_vecInt.end(),mem_fun(&foo::printInt));
}
[snip]

A simplest solution is to make foo::printInt() a static or non-member
function since it doesn't need anything but the function parameter that
you pass in.

Cheers! --M
 
P

peter steiner

IndyStef said:
I am trying to use boost's bind on a member function, on the VC8
compiler.
After using several different attempts, I could not get it to work.
Does
anybody know what is wrong with the code below? The function that
doesn't
compile is foo::DoTheStuff. All three variations of the for-each loop
won't
build.

Thank you,
Stefan

// Boost_Bind.cpp : Defines the entry point for the console
application.
//

#include "stdafx.h"
#include <boost/bind.hpp>
#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>

using namespace std;

class foo
{
public:
foo() {};
virtual ~foo() {};

void DoTheStuff();
void PrintInt( int a_nI );

protected:
vector<int> m_vecInt;
};

void foo::printInt(int a_nI )
{
cout << a_nI << endl;
}

void foo::DoTheStuff()
{
m_vecInt.reserve(50);
generate(m_vecInt.begin(),m_vecInt.end(),rand);

for_each(m_vecInt.begin(),m_vecInt.end(),boost::bind(&foo::printInt,
_1));

you forgot to additionally bind your instance of foo.

try:

for_each(m_vecInt.begin(),m_vecInt.end(),boost::bind(&foo::printInt,
this, _1));

bind returns a generalized function object which knows nothing about
existing inside a function instance (and thus the current this
pointer), you have to explicitely bind to instance (this) and function
(foo::printInt).

btw the boost mailing list is also available as newsgroup...

-- peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top