Boost logo

Boost Users :

Subject: Re: [Boost-users] boost::function/boost::bind compilation error
From: Stuart Dootson (stuart.dootson_at_[hidden])
Date: 2009-09-05 20:30:29


On Sun, Sep 6, 2009 at 1:23 AM, Ahmed Badran<ahmed.badran_at_[hidden]> wrote:
> Hi,
>
> I was trying to use boost function/bind to bind the first argument of a
> member function that takes two arguments, when I got into a compilation
> error that I can't quite decipher or understand, below is a sample
> representation of the problem, and then there's the compilation error, am I
> doing something incorrectly here?
>
> I have tried removing the boost::mem_fn instantiation with no difference.
>
>
> #include <boost/function.hpp>
> #include <boost/bind.hpp>
> #include <boost/mem_fn.hpp>
> #include <iostream>
>
> using namespace std;
> using namespace boost;
>
> class my_class
> {
>        public:
>        void test(bool b)
>        {
>                cout << "got " << b << endl;
>        }
>        private:
> };
>
>
>
> int main()
> {
>        my_class c;
>        boost::function1<void, bool> test_fn;
>        boost::bind(&my_class::test, &c, _2);
>
>        /* causes a compilation error */
>        test_fn = boost::bind<void>(boost::mem_fn(&my_class::test), &c, _2);

Don't need the mem_fn here - and the _2 should be _1 as well, as
you've declared test_fn with 1 parameter, so use this:

       test_fn = boost::bind(&my_class::test, &c, _1);

>        /* works fine */
>        test_fn = bind1st(mem_fun(&my_class::test), &c);
>
>        test_fn(true);
>        test_fn(false);
>        return 0;
> }
>

<snip>

>
>
> Regards,
> Ahmed

Stuart Dootson


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net