我想让这段代码正常工作,我该怎么做?
在最后一行给出这个错误。
我做错了什么?我知道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*/
}发布于 2010-06-01 12:18:47
你遇到的问题是你试图绑定到一个模板化的函数。在这种情况下,您需要指定要调用以绑定的方法的模板类型。
这种情况发生在A::bindA方法上。参见下面的main代码片段,该代码片段可以使用所提供的类正确编译。
顺便说一下,在这个示例中,我使用了boost::function (绑定的姊妹库)来指定所使用的函数指针的类型。我认为这使它更具可读性,如果您打算继续使用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);
}https://stackoverflow.com/questions/2947151
复制相似问题