简体   繁体   中英

error with member function pointers, boost bind

I coded a client which connects to a server in a single file without any classes. This obviously isnt a good way of programming, so when the code finally behaved as i want, i created the myclient class with the following client.h :

    #include <boost/asio.hpp> 
    #include <boost/array.hpp> 
    #include <iostream> 
    #include <string> 
    #include "mtbf.h"
    #include <boost/date_time/posix_time/posix_time.hpp>

    class myclient
    {
    public:
    boost::asio::io_service io_service; 
    boost::asio::ip::tcp::resolver resolver; 
    boost::asio::ip::tcp::socket sock; 
    boost::array<char, 4096> buffer; 
    typedef boost::posix_time::time_duration duration_t;
    typedef boost::posix_time::ptime timestamp_t;
    std::vector<std::string> tokens;
    boost::system::error_code m_err_feedback;



            enum ConnectionState
        {
            WAIT_FOR_HEADER,
            WAIT_FOR_DATA,
            PACKET_COMPLETE
        };
    myclient(std::string port);
    private:
        void read_handler(const boost::system::error_code &ec, std::size_t bytes_transferred);
        void connect_handler(const boost::system::error_code &ec);
        void resolve_handler(const boost::system::error_code &ec, boost::asio::ip::tcp::resolver::iterator it);

    };

I dont write the full code of the client.cpp, but here is where the error occurs(constructor)

    myclient::myclient(std::string port):resolver(io_service),sock(io_service)
    {
        tcp::resolver::query query("127.0.0.1", port); 
        resolver.async_resolve(query, boost::bind(&myclient::resolve_handler, this)); 
        io_service.run(); 
    }

I am fairly new to c++, but i get that i need a boost::bind to call the reference for a member function in

 resolver.async_resolve(query, boost::bind(&myclient::resolve_handler, this)); 

but somehow this isnt quite right..i get an error :

error   8   error C2298: 'return': Invalid procedure for an expression with pointer to memberfunction C:\projekte\libs\boost_1_53_0-VC11\boost\bind\mem_fn.hpp  342 1   client

EDIT: Thanks for your reply..but it seems there are just more errors coming. Am i missing something trivial? Here is the code from client.cpp ( i removed the actual algorithm in read_handler because its unimportant )

    #include "client.h"
    using boost::asio::ip::tcp;

    myclient::myclient(std::string port):resolver(io_service),sock(io_service)
    {
        tcp::resolver::query query("127.0.0.1", port); 
        resolver.async_resolve(query, boost::bind(&myclient::resolve_handler, this,boost::asio::placeholders::error,boost::asio::placeholders::iterator)); 
        io_service.run(); 
    }

    void myclient::read_handler(const boost::system::error_code &ec, std::size_t bytes_transferred) 
    { 

    .
    .
    .


    } 

    void myclient::connect_handler(const boost::system::error_code &ec) 
    { 
      if (!ec) 
      { 
        sock.async_read_some(boost::asio::buffer(buffer), boost::bind(&myclient::read_handler, this,&ec)); 
      } 
    } 

    void myclient::resolve_handler(const boost::system::error_code &ec, boost::asio::ip::tcp::resolver::iterator it) 
    { 
      if (!ec) 
      { 
        sock.async_connect(*it, boost::bind(&myclient::connect_handler, this, &ec, it)); 
      } 
    } 

with this i get errors like :

error   2   error C2825: "F": must be a class or namespace "::".    C:\projekte\libs\boost_1_53_0-VC11\boost\bind\bind.hpp  69  1   client
error   3   error C2039: 'result_type': is not an element of '`global namespace''   C:\projekte\libs\boost_1_53_0-VC11\boost\bind\bind.hpp  69  1   client

You're forgetting the placeholders for the arguments to the resolve_handler :

boost::bind(&myclient::resolve_handler, this,
    boost::asio::placeholders::error, 
    boost::asio::placeholders::iterator)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM