tags:

views:

112

answers:

1

I want to make this code work properly, what should I do?

giving this error on the last line.

what am I doing wrong? i know boost::bind need a type but i'm not getting. help

class A
{

public:

    template <class Handle>
    void bindA(Handle h)
    {
        h(1, 2);
    }
};

class B
{

    public:
        void bindB(int number, int number2)
        {
            std::cout << "1 " << number << "2 " << number2 << std::endl;
        }
};


template < class Han > struct Wrap_
{

    Wrap_(Han h) : h_(h) {}

    template<typename Arg1, typename Arg2> void operator()(Arg1 arg1, Arg2 arg2)
    {
        h_(arg1, arg2);
    }
    Han h_;
};

template< class Handler >

    inline Wrap_<Handler> make(Handler h)
    {
        return Wrap_<Handler> (h);
    }
int main()
{

    A a;
    B b;
    ((boost::bind)(&B::bindB, b, _1, _2))(1, 2);
    ((boost::bind)(&A::bindA, a, make(boost::bind(&B::bindB, b, _1, _2))))();
/*i want compiled success and execute success this code*/

}
+2  A: 

The problem that you are having is that you are trying to bind to a templated function. In this case you need to specify the template type of the method you are calling to bind.

This is happening for the method A::bindA. See below for a code fragment for main that compiles correctly with the supplied classes.

Incidentally in the example I use boost::function (the sister library to bind) to specify what type of function pointers are being used. I think this makes it far more readable and would highly recommend that you become familiar with it if you are going to continue using bind.

#include "boost/bind.hpp"
#include "boost/function.hpp"

int main(int c, char** argv)
{
  A a;
  B b;

  typedef boost::function<void(int, int)> BFunc;
  typedef boost::function<void(BFunc)> AFunc;
  BFunc bFunc( boost::bind(&B::bindB, b, _1, _2) );
  AFunc aFunc( boost::bind(&A::bindA<BFunc>, a, make(bFunc)) );

  bFunc(1,2);
}
radman