title: Create pointer to boost asio tcp socket
body: <h2>What I'm trying to make</h2>
  
  <p>I'm working on a library that connects to a piece of hardware over TCP. The library provides some functions for setting parameters on the remote hardware. To make this library easy to use I want the connect function to create a connection state <code>struct</code> / object, a pointer to which will be held by the software calling using this class. That way it can connect to multiple targets at once and have the handles for each connection to pass to the other functions of the library.</p>
  
  <p>Here is the relevant source code:</p>
  
  <pre><code>#include &lt;boost/array.hpp&gt;
  #include &lt;boost/asio.hpp&gt;
  using boost::asio::ip::tcp;
  
  struct connection_state {
      boost::asio::io_service *io_service;
      tcp::socket *socket;
      bool connected = false;
  };
  
  __stdcall int connect(void **state, const char *host, int port) {
      // create connection state object
      struct connection_state *s = (connection_state *)calloc(1, sizeof(struct connection_state));
      if (!s) {
          // we did not get the memory
          return -ENOMEM;
      }
      // bend the state pointer to point to our new object
      *state = (void *)s;
      // create ioservice object and put it in state
      s-&gt;io_service = new boost::asio::io_service();
      // create socket and put it in state
      s-&gt;socket = new tcp::socket(s-&gt;io_service); // THIS LINE FAILS
      // create resolver on stack
      tcp::resolver resolver(s-&gt;io_service);
      // create query
      tcp::resolver::query query(host, std::to_string(port));
      tcp::resolver::iterator itt;
      // resolve IP / hostname
      itt = resolver.resolve(query);
      // connect!
      boost::asio::connect(s-&gt;socket, itt);
      // we are now connected!
      s-&gt;connected = true;
      return 0;
  }
  </code></pre>
  
  <p>This code is based on the <a href="http://www.boost.org/doc/libs/1_65_1/doc/html/boost_asio/tutorial/tutdaytime1.html" rel="nofollow noreferrer">example TCP client</a> from the boost documentation, but I'm trying to have the <code>socket</code> and <code>io_service</code> objects in some form of object I can pass around.</p>
  
  <h2>My Problem</h2>
  
  <p>This fails to build with:</p>
  
  <pre><code>pc/PC.cpp: In function ‘int connect(void**, const char*, int)’: 
  pc/PC.cpp:67:44: error: no matching function for call to 
  ‘boost::asio::basic_stream_socket&lt;boost::asio::ip::tcp&gt;::basic_stream_socket(boost::asio::io_service*&amp;)’
   s-&gt;socket = new tcp::socket(s-&gt;io_service); // THIS LINE FAILS
                                            ^
  </code></pre>
  
  <p>I don't really know why I'm unable to create a new <code>tcp::socket</code> this way.</p>