简体   繁体   中英

C++/Boost: Passing a member function as a parameter to be used in boost::bind

I'm pretty new to C++, so please, don't judge me :-)

I've a class called connection that includes the following methods:

    void connection::handle_connect(const boost::system::error_code& err) {
        if (!err) {
            boost::asio::async_write(socket_, request_,
                boost::bind(&connection::handle_write_request, this,
                boost::asio::placeholders::error, &connection::handle_read_status_line));
        } else {
            std::cout << "Error in handle_connect: " << err.message() << "\n";
        }
    }

    void connection::handle_write_request(const boost::system::error_code& err, boost::function<void(const boost::system::error_code&)> callback) {
        if (!err) {
            boost::asio::async_read_until(socket_, response_, 0,
                boost::bind(&callback, this,
                boost::asio::placeholders::error));
        } else {
            std::cout << "Error in handle_write_request: " << err.message() << "\n";
        }
    }

    void connection::handle_read_status_line(const boost::system::error_code& err) {
        if (!err) {
            std::istream response_stream(&response_);
            std::string response;
            std::getline(response_stream, response);
            std::cout << "Response: " << response << std::endl;
        } else {
            std::cout << "Error in handle_read_status_line: " << err << "\n";
        }
    }

and I'm trying to make the handle_write_request method more versatile. I've added callback to its signature - this should be an address of a method that is passed to boost::bind and called as a callback in boost::asio::async_read_until .

However, this cannot be even compiled :-) MSVS2013 is saying

    Error   1   error C2825: 'F': must be a class or namespace when followed by '::'    C:\local\boost_1_55_0\boost\bind\bind.hpp   69  1   driver
    Error   2   error C2039: 'result_type' : is not a member of '`global namespace''    C:\local\boost_1_55_0\boost\bind\bind.hpp   69  1   driver
    Error   3   error C2146: syntax error : missing ';' before identifier 'type'    C:\local\boost_1_55_0\boost\bind\bind.hpp   69  1   driver
    Error   4   error C2208: 'boost::_bi::type' : no members defined using this type C:\local\boost_1_55_0\boost\bind\bind.hpp  69  1   driver
    Error   5   error C1903: unable to recover from previous error(s); stopping compilation C:\local\boost_1_55_0\boost\bind\bind.hpp   69  1   driver

How should I pass the address of the member function properly? I'll be grateful for any suggestions!

To bind a class's member function, do this:

boost::bind(&SomeClass::SomeMemberFunc, this, params...);

In your case, callback is already a functor, so try boost::bind(callback, this, <some proper parameter>);

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