title: zeroMQ client exercise separating interface from implementation
body: <p>I am testing the examples of the ZeroMQ framework asynchronous library messaging to work in distributed applications, enabling the interoperability between programming languages.</p>
  
  <p>I write the following C++ client code <a href="http://zguide.zeromq.org/cpp:hwclient" rel="nofollow noreferrer">based in this code</a> </p>
  
  <pre><code>#include &lt;zmq.hpp&gt;
  #include &lt;iostream&gt;
  #include &lt;sys/time.h&gt;
  
  using std::cout;
  using std::cin;
  using std::endl;
  using std::string;
  
  int main ()
  {
      //  Prepare our context and socket
      zmq::context_t context (1);
      zmq::socket_t socket (context, ZMQ_REQ);
      struct timeval t_ini, t_fin;
  
      gettimeofday(&amp;t_ini, NULL);
  
      cout &lt;&lt; "Connecting to zeroMQ python server" &lt;&lt; endl;
      socket.connect ("tcp://localhost:5555");
  
      // Send data to server socket
      zmq::message_t request (20);
      memcpy (request.data (), "Hola Servidor Python", 20);
      socket.send (request);
  
      // Getting reply form zeroMQ python server
      zmq::message_t reply;
      socket.recv (&amp;reply);
      string replyMessage = string(static_cast&lt;char *&gt;(reply.data()), reply.size());
  
      cout &lt;&lt; "Getting reply form zeroMQ python server: " + replyMessage &lt;&lt; " "  &lt;&lt; endl;
  
      gettimeofday(&amp;t_fin, NULL);
  
      printf("Tiempo en microsegundos: %ld microsegundos\n",
             ((t_fin.tv_sec - t_ini.tv_sec)*1000000L
             +t_fin.tv_usec) -t_ini.tv_usec
            );
      return 0;
  
  }
  </code></pre>
  
  <p>This c++ client code it's works</p>
  
  <p>My objective is rewrite the previous code separating interface from implementation making use of C++ headers files.</p>
  
  <p>I have the <code>client.h</code> file in which I define the <code>class Client</code></p>
  
  <pre><code>#ifndef ZEROMQ_CLASS_CLIENT_H
  #define ZEROMQ_CLASS_CLIENT_H
  #include &lt;sys/time.h&gt;
  #include &lt;iostream&gt;
  
  using std::string;
  
  class Client{
  public:
      Client();
      void setupConnection();
  
  private:
      //zmq::context_t context (1);
      //zmq::socket_t socket (context, ZMQ_REQ);
      struct timeval t_before, t_after;
      string replyMessage;
  
  };
  
  #endif //ZEROMQ_CLASS_CLIENT_H
  </code></pre>
  
  <p>I have the <code>client.cpp</code> file in which I develop my <code>setupConnection()</code> members function</p>
  
  <pre><code>#include "client.h"
  #include &lt;zmq.hpp&gt;
  
  using std::cout;
  using std::endl;
  
  
  
  Client::Client(){
  
  };
  
  void Client::setupConnection(){
  
      gettimeofday(&amp;t_before, NULL);
  
      cout &lt;&lt; "Connecting to python zeroMQ server ..."&lt;&lt;endl;
      zmq::context_t context(1);
      zmq::socket_t socket (context, ZMQ_REQ);
      socket.connect("tcp://localhost:5555");
  
      zmq::message_t request (20);
      memcpy(request.data(), "Hello Python server", 20);
      socket.send(request);
  
      zmq::message_t reply;
      socket.recv(&amp;reply);
      replyMessage = string(static_cast&lt;char *&gt;(reply.data()), reply.size());
  
      cout &lt;&lt; "Getting response from server: " + replyMessage &lt;&lt; " " &lt;&lt; endl;
      gettimeofday(&amp;t_after, NULL);
  
      printf("Time in microseconds: %ld microseconds\n",
             ((t_after.tv_sec - t_before.tv_sec)*1000000L
              +t_before.tv_usec) -t_before.tv_usec
      );
  }
  </code></pre>
  
  <p>And in the <code>main.cpp</code> file I create the object and call to the function:</p>
  
  <pre><code>#include "client.h"
  
  int main() {
      Client c;
      c.setupConnection();
      return 0;
  }
  </code></pre>
  
  <p>When I build the <code>main.cpp</code> file, I get this message:</p>
  
  <pre><code>% g++ main.cpp -o client.out -lzmq
  /tmp/cc8hDANf.o: In function `main':
  main.cpp:(.text+0x20): undefined reference to `Client::Client()'
  main.cpp:(.text+0x2c): undefined reference to `Client::setupConnection()'
  collect2: error: ld returned 1 exit status
  </code></pre>
  
  <p>I can't figure out what's wrong with my code, any idea?</p>