1

このコードを正しく動作させたいのですが、どうすればよいですか?

最後の行でこのエラーを出します。

私は何を間違っていますか?boost::bind にはタイプが必要であることはわかっていますが、得られません。ヘルプ

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*/

}
4

1 に答える 1

2

あなたが抱えている問題は、テンプレート化された関数にバインドしようとしていることです。この場合、バインドするために呼び出すメソッドのテンプレート タイプを指定する必要があります。

これは method で発生していA::bindAます。提供されたクラスで正しくコンパイルされる main のコード フラグメントについては、以下を参照してください。

ちなみに、この例では、boost::function (バインドする姉妹ライブラリ) を使用して、使用する関数ポインターの型を指定しています。これにより、はるかに読みやすくなると思います。バインドを引き続き使用する場合は、慣れておくことを強くお勧めします。

#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);
}
于 2010-06-01T04:18:47.390 に答える