c++boostboost-asioboost-bind

boost bind with member function in asio spawn call


in the following code i make map,
then add some httprequest class instances to it,
then i call boost asio spawn using member function of httprequest class,
i asked about this before and told it was duplicate ,
so i read the post and made some digging,and found the following:
i have to use boost::bind to convert member function and its arguments to single function object.
and inside boost bind i have to add address of the instance on which the method works,and address of method"do not get if address of method from class definition or from the used instance,
then add the arguments. my map has httprequests represented by shared pointers.
when i write this and compile it gives me lots of errors related to bind.
the problem is stated to be in bind file which represent my code but does not say what is the part having problem.

here is the code:

std::map<std::string, boost::shared_ptr<HTTPRequest>> requests_variables;
std::map<std::string, boost::shared_ptr<HTTPResponse>> tasks;    

for (int hour = 0;hour < 24;hour++)
{
    std::stringstream ss_hour;
    ss_hour << std::setw(2) << std::setfill('0') << hour;
    std::string url_hour{ ss_hour.str() };
    std::stringstream().swap(ss_hour); // swap m with a default constructed stringstream
    URL = URL + url_hour +"h_ticks.bi5";

    std::string request_name = "request_" + std::to_string(hour);
    requests_variables[request_name] =  client.create_request(hour, URL);       
    boost::asio::spawn(client.get_service_reference(), boost::bind(&(*requests_variables[request_name]),&(*requests_variables[request_name]).execute, boost::placeholders::_1, tasks, request_name, requests_variables));

i tried to manipulate it many times to get it compile but in vain.
something is wrong with the bind but i can't get to it.
thanks in advance..


Solution

  • i do not know why, but this made it work.

    HTTPRequest* requst_z = requests_variables[request_name].get();
    
            boost::asio::spawn(client.get_service_reference(), boost::bind(&HTTPRequest::execute, requst_z, boost::placeholders::_1, tasks, request_name, requests_variables));