我正在编译这个简单的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
答案 0 :(得分:0)
acc
变量仅在main
范围内。
你需要把它变成一个全局变量 - 定义一个类中的所有东西。
class myAcceptor
{
io_service service;
ip::tcp::endpoint ep;
ip::tcp::acceptor acc;
socket_ptr sock;
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();
}
答案 1 :(得分:0)
将acc
设为全局(不推荐),或将其传递给函数:
void start_accept(socket_ptr sock, ip::tcp::acceptor &acc)
{
acc.async_accept(*sock, boost::bind( handle_accept, sock, _1) );
}
答案 2 :(得分:0)
test3.cpp:31: error: ‘acc’ was not declared in this scope
fredrik和MM是什么。说。
test3.cpp:31: error: ‘handle_accept’ was not declared in this scope
自start_accept depends
上的handle_accept
开始,反之亦然,您必须转发声明一个。在您的代码中,请在handle_accept
之前声明start_accept
。
test3.cpp:38: error: declaration of ‘socket_ptr sock’ shadows a parameter
test3.cpp:38: error: ‘service’ was not declared in this scope
您可能只想将sock
参数传递给start_accept
?但是,这个功能看起来很无趣。
另外,我不得不说,如果没有能够阅读这些非常基本的编译错误,那么你就会很难用asio和提升。