为什么这"boost::bind"不编译?
why doesn't this "boost::bind" compile?
我正在尝试使用boost::bind和boost::函数,但出现了编译错误。为什么不能将占位符绑定到函数对象?
void fun_1(const boost::system::error_code& error)
{
std::cout<<"test_1------------"<<std::endl;
}
void fun_2(int i)
{
std::cout<<"tset_2------------"<<std::endl;
}
int main(void)
{
boost::function0<void> fobj;
//fobj = boost::bind(&fun_1,boost::asio::placeholders::error);//compile failed
fobj = boost::bind(&fun_2,5);//syntax is ok
return 0;
}
您的签名不匹配。试试这个:
#include <boost/function.hpp>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
void fun_1(const boost::system::error_code& error)
{
std::cout<<"test_1------------"<<std::endl;
}
void fun_2(int i)
{
std::cout<<"tset_2------------"<<std::endl;
}
int main(void)
{
boost::function<void (const boost::system::error_code&)> fobj;
fobj = boost::bind(&fun_1,boost::asio::placeholders::error);
return 0;
}
用g++ main.cpp -lboost_system -lpthread
在gcc下进行编译测试
为了将来参考,添加有助于
- 完全可编译的示例
- 完整的编译/链接器错误消息
当发布如上所述的片段以节省想要帮助您的人的时间时。
如果无法更改签名,则需要传递要绑定的error_code
的值,而不是占位符,以便在调用函数时提供该值
即:
#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <boost/asio.hpp>
#include <boost/system/error_code.hpp>
void fun_1(const boost::system::error_code& error)
{
std::cout<<"test_1------------"<<std::endl;
}
void fun_2(int i)
{
std::cout<<"tset_2------------"<<std::endl;
}
int main()
{
boost::function0<void> fobj;
//fobj = boost::bind(&fun_1,boost::asio::placeholders::error);//compile failed
fobj = boost::bind( &fun_1,
boost::system::errc::make_error_code( boost::system::errc::success ) );//compiles ok
// fobj = boost::bind(&fun_2,5);//syntax is ok
return 0;
}
注意:通过查看http://my.oschina.net/yellia/blog/90678,作者简单明了地解释了boost::bind的实现。我希望它能帮助你。哈哈
相关文章:
- 二叉排序树无法编译
- 编译时未启用intel oneApi CUDA支持
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- 试图克服 std::bind 编译错误
- 为什么 std::bind 绑定到成员函数时无法编译?
- 在编译时获取 std::bind 的类型
- 在spirit parser Action中,在上下文参数上编译boost :: bind(成员函数)上的错误
- 在 std::bind 中使用 std::bind :编译错误(隐式强制转换)
- 为什么这"boost::bind"不编译?
- C 11 std :: bind and auto的编译错误,用于回调函数参数
- 编译 Boost.Bind时出错
- 为什么boost::asio::io_service不使用std::bind编译
- boost bind.hpp 1.54 c++ 专用模板无法在 Solaris 上编译。有效C++?
- 编译使用 libcxx 的套接字函数 bind() 的代码失败
- 对std::string::find使用boost::bind无法编译
- 如何使用std::bind编译c++程序
- std::bind 在 libstdc++ 中产生编译错误
- 编译时是否可以对std::bind进行类型检查
- boost::bind到std::lower_bound可以工作,但是c++ 0x std::bind无法编译
- Std::bind上的Std::result_of不能在clang++ 3.4上编译