boost::bind problem in VC8

Discussion in 'C++' started by IndyStef, Nov 30, 2005.

  1. IndyStef

    IndyStef Guest

    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;
    }
     
    IndyStef, Nov 30, 2005
    #1
    1. Advertising

  2. IndyStef

    mlimber Guest

    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::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
     
    mlimber, Nov 30, 2005
    #2
    1. Advertising

  3. 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::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
     
    peter steiner, Nov 30, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    460
    Victor Bazarov
    Mar 10, 2006
  2. Toby Bradshaw
    Replies:
    6
    Views:
    1,813
    Kai-Uwe Bux
    Jun 2, 2006
  3. Replies:
    0
    Views:
    614
  4. Christopher
    Replies:
    1
    Views:
    861
    Yakov Gerlovin
    Oct 5, 2011
  5. Mark
    Replies:
    1
    Views:
    468
    Jeff Flinn
    Nov 25, 2012
Loading...

Share This Page