2

i simply made class with shared pointer to vector of end point as 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);  
};

then i made method to get this shared pointer which return shared pointer ,

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

in constructor of other class which is declared to take argument as shared pointer to vector i pass the previous method .

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

}

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.

this is client class showing its methods and members:

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;

};
ahmed allam
  • 377
  • 2
  • 15

1 Answers1

0

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.

That's not a problem. std::vector<T> is always equivalent to std::vector<T, std::allocator<T> > (because of the default template argument: http://en.cppreference.com/w/cpp/container/vector

It is not a problem:

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

Bonus

It seems likely that you only want the client to hold a single endpoint (guessing here). Meet the aliasing constructor: 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);
    }
}
sehe
  • 374,641
  • 47
  • 450
  • 633
  • i added const to get_endpoints method both in header and cpp files...but intellisense still underlines it by red line and say argument mismatch...i will add std allocator to definitions and see what happens – ahmed allam Apr 29 '18 at 08:38
  • Again. The allocator is not the problem. It's always implicitly there. Does the program compile/run? Many IDE s have trouble with accurate intellisense – sehe Apr 29 '18 at 08:49
  • i cant compile now because i changed all my classes and there is alot of errors to correct...but i think the problem is related to intellisense because even after i added allocators to match the error message ...it still gives same error – ahmed allam Apr 29 '18 at 08:53
  • Time to 1. Go back to the last working version and two the changes in smaller steps or 2. Present a self contained example of the code that reproduces the issue. Good luck – sehe Apr 29 '18 at 08:58
  • in your code i find something very interesting ...in both structs you made private two sentences,the first SharedEndpoints _endpoints = std::make_shared();and the second SharedEndpoint _endpoint; without make_shared and ....i think the second one is only declaration but you pass variables to it...and also give it name....is this declaration or definition???....and if it is definition why it is not initialized???"class definitions must be initialized i think" – ahmed allam May 01 '18 at 06:38
  • Time to [read a book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Non-static members are initialized in the constructor. The only "exception" (syntactically) would be NSMI, where the initializer is specified with the member declaration. However, the semantics are exactly the same. Missing initializers for members lead to default-initialization. – sehe May 01 '18 at 10:39