gpt4 book ai didi

c++ - 在asio spawn调用中 boost 与成员函数的绑定(bind)

转载 作者:太空宇宙 更新时间:2023-11-04 12:48:59 24 4
gpt4 key购买 nike

在下面的代码中我制作了 map ,
然后向其中添加一些httprequest类实例,
然后我使用 httprequest 类的成员函数调用 boost asio spawn,
我之前问过这个并告诉它是重复的,
所以我阅读了帖子并进行了一些挖掘,发现了以下内容:
我必须使用 boost::bind 将成员函数及其参数转换为单个函数对象。
在 boost 绑定(bind)中,我必须添加该方法在其上运行的实例的地址,并且方法地址“如果方法地址来自类定义或使用的实例,则不会获取,
然后添加参数。我的 map 有由共享指针表示的 httprequests。
当我写这个并编译它时,它给了我很多与绑定(bind)相关的错误。
问题被声明在代表我的代码的绑定(bind)文件中,但没有说明有问题的部分。

代码如下:

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));

我试图多次操纵它以使其编译但徒劳无功。
绑定(bind)有问题,但我无法访问它。
提前致谢..

最佳答案

我不知道为什么,但这使它起作用了。

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));

关于c++ - 在asio spawn调用中 boost 与成员函数的绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49967064/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com