gpt4 book ai didi

c++ - 指向 vector 的共享指针返回包含标准分配器的不同共享指针

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:41:12 27 4
gpt4 key购买 nike

我只是将带有指向端点 vector 的共享指针的类设为私有(private),

class HTTPService_resolve
{
public:
HTTPService_resolve();
HTTPService_resolve(std::string);
boost::shared_ptr<std::vector<boost::asio::ip::tcp::endpoint>> resolve_func();

boost::shared_ptr<std::vector<boost::asio::ip::tcp::endpoint>> resolve_func(std::string);
boost::asio::io_service& get_service_reference();
boost::shared_ptr<std::vector<boost::asio::ip::tcp::endpoint>> get_vec_endpoints_ptr();

private:
boost::asio::io_service service_resolve_ios;
std::string service_resolve_protocol_host_URL;
std::string host_name;
std::string port_service;
boost::asio::ip::tcp::resolver protocol_host_URL_resolver{ service_resolve_ios };
boost::shared_ptr<std::vector<boost::asio::ip::tcp::endpoint>> vec_endpoints_ptr = boost::make_shared<std::vector<boost::asio::ip::tcp::endpoint>>();//(new HTTPResponse);
};

然后我创建了方法来获取返回共享指针的共享指针,

boost::shared_ptr<std::vector<boost::asio::ip::tcp::endpoint>> HTTPService_resolve::get_vec_endpoints_ptr()
{
return vec_endpoints_ptr;
}

在声明为将参数作为指向 vector 的共享指针的其他类的构造函数中,我传递了先前的方法。

HTTPClient::HTTPClient(const symbols_enum symbol = symbols_enum::EURUSD, date day = date(2012, Jan, 10), boost::shared_ptr<std::vector<boost::asio::ip::tcp::endpoint>> client_vec_endpoints_ptr) :
symbol(symbol),
day(day),
client_vec_endpoints_ptr(client_vec_endpoints_ptr)
{

}

但 intellisense 告诉我参数不同。
参数是指向端点 vector 和标准分配器的共享指针,
并且由于标准分配器部分,它给出了错误。
我不知道为什么 vector 变成了一个包含 std 分配器的部分,我什至不知道什么是 std 分配器并且以前从未使用过它。

这是显示其方法和成员的客户端类:

class HTTPClient
{
friend class HTTPService_resolve;

public:
/*
// set up the worker threads in a thread group
22 boost::thread_group workers;
23 for (int i = 0; i < 3; ++i) {
24 workers.create_thread([&service, &mtx]() {
25 PRINT_ARGS("Starting worker thread ");
26 service.run();
27 PRINT_ARGS("Worker thread done");
28 });
29 }

*/
HTTPClient(const symbols_enum, date);
HTTPClient(const symbols_enum, date, boost::shared_ptr<std::vector<boost::asio::ip::tcp::endpoint>>);

boost::asio::io_service& HTTPClient::get_service_reference();

boost::shared_ptr<HTTPRequest> create_request(unsigned int);
boost::shared_ptr<HTTPRequest> create_request(unsigned int, std::string);
//void create_tasks(const symbols_enum&, date);
void create_tasks(const symbols_enum , date);
void fetch_day();
void close();
private:
//boost::asio::io_service m_ios;//HTTPService_resolve>>1
boost::asio::io_service m_ios;
std::unique_ptr<boost::asio::io_service::work> m_work;
std::unique_ptr<boost::thread> m_thread;
symbols_enum symbol = symbols_enum::EURUSD;
date day = date(2012, Jan, 10);
boost::shared_ptr<std::vector<boost::asio::ip::tcp::endpoint>> client_vec_endpoints_ptr;

};

最佳答案

but intellisense tells me that the argument is different. the argument is shared pointer to vector of endpoint and std allocator, and because of std allocator part ,it gives error. i do not know why the vector changed to one taking std allocator part,i even do not know what is std allocator and never used it before.

这不是问题。 std::vector<T>总是等同于 std::vector<T, std::allocator<T> > (因为默认模板参数:http://en.cppreference.com/w/cpp/container/vector

这不是问题:

Live On Coliru

#include <memory>
#include <vector>
#include <boost/asio.hpp>

enum class Symbol { EURUSD };
using Date = boost::gregorian::date;

using Endpoint = boost::asio::ip::tcp::endpoint;
using Endpoints = std::vector<Endpoint>;

using SharedEndpoint = std::shared_ptr<Endpoint>;
using SharedEndpoints = std::shared_ptr<Endpoints>;

struct HTTPService {
SharedEndpoints get_endpoints() const { return _endpoints; }
private:
SharedEndpoints _endpoints = std::make_shared<Endpoints>();
};

struct HTTPClient {
HTTPClient(Symbol sym, Date date, SharedEndpoints endpoints) : _sym(sym), _date(date), _endpoints(endpoints) {}

private:
Symbol _sym;
Date _date;
SharedEndpoints _endpoints;
};

int main() {
HTTPService svc;
HTTPClient client1(Symbol::EURUSD, Date(2012, 1, 1), svc.get_endpoints());
HTTPClient client2(Symbol::EURUSD, Date(2012, 1, 2), svc.get_endpoints());
}

奖金

您似乎只希望客户端持有一个端点(此处猜测)。认识别名 构造函数:http://en.cppreference.com/w/cpp/memory/shared_ptr/shared_ptr

Live On Coliru

#include <memory>
#include <vector>
#include <boost/asio.hpp>

enum class Symbol { EURUSD };
using Date = boost::gregorian::date;

using Endpoint = boost::asio::ip::tcp::endpoint;
using Endpoints = std::vector<Endpoint>;

using SharedEndpoint = std::shared_ptr<Endpoint>;
using SharedEndpoints = std::shared_ptr<Endpoints>;

struct HTTPService {
SharedEndpoints get_endpoints() const { return _endpoints; }
private:
SharedEndpoints _endpoints = std::make_shared<Endpoints>();
};

struct HTTPClient {
HTTPClient(Symbol sym, Date date, SharedEndpoint endpoint) : _sym(sym), _date(date), _endpoint(endpoint) {}

private:
Symbol _sym;
Date _date;
SharedEndpoint _endpoint;
};

int main() {
HTTPService svc;

std::vector<HTTPClient> clients;
auto shared_endpoints = svc.get_endpoints();
for(auto ep : *shared_endpoints) {
// alias a shared endpoint pointer to the lifetime of the endpoint vector
SharedEndpoint sep(shared_endpoints, &ep);

clients.emplace_back(Symbol::EURUSD, Date(2012, 1, 1), sep);
}
}

关于c++ - 指向 vector 的共享指针返回包含标准分配器的不同共享指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50075188/

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