读书人

抄《 Boost程序库完全开发指南深入

发布时间: 2012-03-13 11:21:12 作者: rapoo

抄《 Boost程序库完全开发指南——深入C++“准”标准库》代码居然编译不过去

C/C++ code
#include <iostream>#include <boost/shared_ptr.hpp>using namespace boost::asio;using namespace boost;using namespace std;class server{private:    io_service &ios;    ip::tcp::acceptor acceptor;        typedef boost::shared_ptr<ip::tcp::socket> sock_pt;public:    server(io_service& io) : ios(io), acceptor(ios, ip::tcp::endpoint(ip::tcp::v4(), 6688))    {        start();    }    void start()    {        sock_pt sock(new ip::tcp::socket(ios));        acceptor.async_accept(*sock, bind(&server::accept_handler, this, aiso::placeholders::error, sock));    }    void accept_handler(const system::error_code& ec, sock_pt sock)    {        if(ec)        { return;}        cout <<"client:";        cout << sock->remote_endpoint().address() <<endl;        sock->async_write_some(buffer("hello asio"),            bind(&server::write_handler, this, aiso::placeholders::error));        start();    }    void write_handler(const system::error_code&)    {        cout << "send msg complete." <<endl;    }};int main(){    try     {        cout << "Server is started." <<endl;        io_service ios;        server serv(ios);        ios.run();    }    catch(std::exception& e)    {        cout <<e.what() <<endl;    }    return 0;}


[解决办法]
boost版本1.42:
http://sourceforge.net/projects/boost/files/boost/1.42.0/boost_1_42_0.zip/download

下载boost源码之后,把boost解压在D盘。

[解决办法]
#include <boost/asio.hpp>
#include <boost/bind.hpp>
aiso -> asio
bind(...) -> boost::bind(...)
VC10自己又整了个std::bind,可能与boost不兼容
boost 1.49 beta1
[解决办法]
同意楼上。
添加
#include <boost/asio.hpp>
#include <boost/bind.hpp>

修改aiso -> asio

本机 vs2008 + boost 1.45 编译通过

读书人网 >C++

热点推荐