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

boost::bind problem in VC8

P: n/a
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::bi nd(&foo::PrintInt,
_1));

//for_each(m_vecInt.begin(),m_vecInt.end(),boost::bi nd<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;
}

Nov 30 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

IndyStef wrote:
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::bi nd(&foo::PrintInt,
_1));

//for_each(m_vecInt.begin(),m_vecInt.end(),boost::bi nd<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

Nov 30 '05 #2

P: n/a
IndyStef wrote:
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::bi nd(&foo::PrintInt,
_1));


you forgot to additionally bind your instance of foo.

try:

for_each(m_vecInt.begin(),m_vecInt.end(),boost::bi nd(&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

Nov 30 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.