-2

我正在编译这个简单的 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 ‘&amp;’ ?)
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
4

3 回答 3

0

test3.cpp:31: error: ‘acc’ was not declared in this scope

什么弗雷德里克和MM。说。

test3.cpp:31: error: ‘handle_accept’ was not declared in this scope

start_accept dependson 开始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 和一般的 boost。

于 2013-03-20T20:42:20.333 回答
0

The acc variable is only in scope in main. You need to make it a global variable - of define everything in a class.

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();
}
于 2013-03-20T20:36:04.273 回答
0

使acc全局(不推荐),或将其传递给函数:

void start_accept(socket_ptr sock,  ip::tcp::acceptor &acc)
{
    acc.async_accept(*sock, boost::bind( handle_accept, sock, _1) );
}
于 2013-03-20T20:38:41.397 回答