473,425 Members | 1,919 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,425 software developers and data experts.

boost::bind problem in VC8

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
2 2245

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

Similar topics

2
by: P G | last post by:
I hope this is on topic here. I have a problem compiling a simple example of the use of boost::bind. Please take a look at the program below. ...
4
by: Arturo Cuebas | last post by:
The program below contains a compile error. Following the program you will find the typical fix then my idea for a library that facilitates a more elegant fix. #include <boost\bind.hpp> using...
0
by: Russell Hind | last post by:
Is it possible to create a boost::bind object for a managed class method? With non-managed classes, I can do boost::function<void ()> f = boost::bind(&Class_c::Function, this); Is there a way...
1
by: Olaf Petzold | last post by:
Hi, at this time I try to combine Qt and boost. Unfortunately I've run into trouble: void QWidget::insertAction ( QAction * before, QAction * action ); class QAction : public QWidget { ... };...
1
by: Tigera | last post by:
Greetings, I am trying to learn to use the Boost library, and I've run into a frustrating problem with the line marked here: #include <boost/function.hpp> #include <boost/functional.hpp>...
0
by: XHengDF | last post by:
I am a new gay to use the library boost, so i confused by some details! now, anybody could help me to explain the difference between boost::bind and boost::lambda::bind when i use the library. I...
3
by: =?iso-8859-1?B?Tm9yZGz2dw==?= | last post by:
Hey there, C++ Coders! I am learning multi-threading with boost and have come up with the following code example shown below. This example implements a test of the producer-consumer design...
1
by: kittymaguire | last post by:
I am using VC2005 and have refined new to be new (_NORMAL_BLOCK ,__FILE__, __LINE__) for the debug build so that the location of memory leaks are reported. The problem that I have is when, I...
3
by: Giovanni Gherdovich | last post by:
Hello, in the following code I have a pointer (to function), say p, of type double (*)(double, double, void*) and I try to fix the second argument of the function *p to a given value (using...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.