C++问题: boost::bind 接收其他 boost::bind
我想让这段代码正常工作,我该怎么办?
在最后一行给出这个错误。
我做错了什么? 我知道 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*/
}
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*/
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您遇到的问题是您正在尝试绑定到模板化函数。在这种情况下,您需要指定要调用绑定的方法的模板类型。
A::bindA
方法就会发生这种情况。请参阅下面的 main 代码片段,该代码片段可以使用提供的类正确编译。顺便说一句,在示例中我使用 boost::function (要绑定的姐妹库)指定正在使用的函数指针的类型。我认为这使它更具可读性,并且如果您要继续使用绑定,强烈建议您熟悉它。
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.