Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > boost::bind problem in VC8

Reply
Thread Tools

boost::bind problem in VC8

 
 
IndyStef
Guest
Posts: n/a
 
      11-30-2005
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:oTheStuff. 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:rintInt(int a_nI )
{
cout << a_nI << endl;
}

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

for_each(m_vecInt.begin(),m_vecInt.end(),boost::bi nd(&foo:rintInt,
_1));

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

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

 
Reply With Quote
 
 
 
 
mlimber
Guest
Posts: n/a
 
      11-30-2005

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:oTheStuff. 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:rintInt(int a_nI )
> {
> cout << a_nI << endl;
> }
>
> void foo:oTheStuff()
> {
> m_vecInt.reserve(50);
> generate(m_vecInt.begin(),m_vecInt.end(),rand);
>
> for_each(m_vecInt.begin(),m_vecInt.end(),boost::bi nd(&foo:rintInt,
> _1));
>
> //for_each(m_vecInt.begin(),m_vecInt.end(),boost::bi nd<void>(mem_fun(&foo:rintInt),
> _1));
> //for_each(m_vecInt.begin(),m_vecInt.end(),mem_fun(& foo:rintInt));
> }

[snip]

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

Cheers! --M

 
Reply With Quote
 
 
 
 
peter steiner
Guest
Posts: n/a
 
      11-30-2005
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:oTheStuff. 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:rintInt(int a_nI )
> {
> cout << a_nI << endl;
> }
>
> void foo:oTheStuff()
> {
> m_vecInt.reserve(50);
> generate(m_vecInt.begin(),m_vecInt.end(),rand);
>
> for_each(m_vecInt.begin(),m_vecInt.end(),boost::bi nd(&foo:rintInt,
> _1));


you forgot to additionally bind your instance of foo.

try:

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

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

-- peter

 
Reply With Quote
 
 
 
Reply

Thread Tools

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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Building extensions with vc8 Howard Lightstone Python 1 01-22-2007 09:48 PM
why the VC8 treat goto and switch-case different? miaohua1982@gmail.com C++ 2 11-28-2006 05:26 AM
How i can make compatible vc7 with vc8 CiberGhost C++ 4 11-06-2006 03:30 PM
Code fails under vc8 - clearing a structure Jack C++ 8 04-03-2006 03:18 PM
problem reading integers separated by commas with VC8 andreas.fabri@geometryfactory.com C++ 4 03-10-2006 09:22 PM



Advertisments