我正在编译这个简单的 boost::asio 异步服务器,它因这个错误而失败,有什么想法吗?
#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/thread.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
using namespace std;
using namespace boost::asio;
using namespace boost::posix_time;
typedef boost::shared_ptr<ip::tcp::socket> socket_ptr;
int main()
{
io_service service;
void start_accept(socket_ptr sock);
void handle_accept(socket_ptr sock, const boost::system::error_code & err);
ip::tcp::endpoint ep( ip::tcp::v4(), 2001);
ip::tcp::acceptor acc(service, ep);
socket_ptr sock(new ip::tcp::socket(service));
start_accept(sock);
service.run();
}
void start_accept(socket_ptr sock)
{
acc.async_accept(*sock, boost::bind( handle_accept, sock, _1) );
}
void handle_accept(socket_ptr sock, const boost::system::error_code & err)
{
if (err) return;
socket_ptr sock(new ip::tcp::socket(service));
start_accept(sock);
}
错误: 编译这个
[root@kris server]# g++ -o test3 test3.cpp -lboost_system -lpthread -lboost_thread-mt
test3.cpp: In function ‘void start_accept(socket_ptr)’:
test3.cpp:31: error: ‘acc’ was not declared in this scope
test3.cpp:31: error: ‘handle_accept’ was not declared in this scope
test3.cpp: In function ‘void handle_accept(socket_ptr, const boost::system::error_code&)’:
test3.cpp:38: error: declaration of ‘socket_ptr sock’ shadows a parameter
test3.cpp:38: error: ‘service’ was not declared in this scope
编译 Freds 版本时出错:
#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/thread.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>
using namespace std;
using namespace boost::asio;
using namespace boost::posix_time;
typedef boost::shared_ptr<ip::tcp::socket> socket_ptr;
class myAcceptor
{
io_service service;
ip::tcp::endpoint ep( ip::tcp::v4(), 2001);
ip::tcp::acceptor acc(service, ep);
socket_ptr sock(new ip::tcp::socket(service));
myAcceptor(): ep(ip::tcp::v4(), 2001),acc(service,ep),
sock(new ip::tcp::socket(service))
{ start_accept(); }
void run()
{
service.run();
}
void start_accept()
{
acc.async_accept(boost::bind( handle_accept, _1) );
}
void handle_accept(const boost::system::error_code & err)
{
if (err) return;
sock = socket_ptr(new ip::tcp::socket(service));
start_accept(sock);
}
};
int main()
{
myAcceptor acceptIt;
acceptIt.run();
}
g++ -o test4 test4.cpp -lboost_system -lpthread -lboost_thread-mt
test4.cpp:18: error: ‘boost::asio::ip::tcp::v4’ is not a type
test4.cpp:18: error: expected identifier before numeric constant
test4.cpp:18: error: expected ‘,’ or ‘...’ before numeric constant
test4.cpp:19: error: ‘service’ is not a type
test4.cpp:19: error: ‘ep’ is not a type
test4.cpp:20: error: expected identifier before ‘new’
test4.cpp:20: error: expected ‘,’ or ‘...’ before ‘new’
test4.cpp: In constructor ‘myAcceptor::myAcceptor()’:
test4.cpp:22: error: class ‘myAcceptor’ does not have any field named ‘ep’
test4.cpp:22: error: class ‘myAcceptor’ does not have any field named ‘acc’
test4.cpp:23: error: class ‘myAcceptor’ does not have any field named ‘sock’
test4.cpp: In member function ‘void myAcceptor::start_accept()’:
test4.cpp:33: error: ‘((myAcceptor*)this)->myAcceptor::acc’ does not have class type
test4.cpp:33: error: no matching function for call to ‘bind(<unresolved overloaded function type>, boost::arg<1>&)’
/usr/include/boost/bind/bind.hpp:1461: note: candidates are: boost::_bi::bind_t<boost::_bi::unspecified, F, typename boost::_bi::list_av_1<A1>::type> boost::bind(F, A1) [with F = void (myAcceptor::*)(const boost::system::error_code&), A1 = boost::arg<1>]
/usr/include/boost/bind/bind.hpp:1710: note: boost::_bi::bind_t<typename boost::_bi::dm_result<M T::*, A1>::type, boost::_mfi::dm<M, T>, typename boost::_bi::list_av_1<A1>::type> boost::bind(M T::*, A1) [with A1 = boost::arg<1>, M = void(const boost::system::error_code&), T = myAcceptor]
test4.cpp: In member function ‘void myAcceptor::handle_accept(const boost::system::error_code&)’:
test4.cpp:39: error: invalid use of member (did you forget the ‘&’ ?)
test4.cpp:40: error: no matching function for call to ‘myAcceptor::start_accept(<unresolved overloaded function type>)’
test4.cpp:31: note: candidates are: void myAcceptor::start_accept()
test4.cpp: In function ‘int main()’:
test4.cpp:22: error: ‘myAcceptor::myAcceptor()’ is private
test4.cpp:45: error: within this context
test4.cpp:26: error: ‘void myAcceptor::run()’ is private
test4.cpp:46: error: within this context