ZEROMQ客户端练习将接口与实现分开
zeroMQ client exercise separating interface from implementation
我正在测试Zeromq Framework的示例异步库消息传递以在分布式应用程序中工作,从而使编程语言之间的互操作性。
我在此代码中编写以下C 客户端代码
#include <zmq.hpp>
#include <iostream>
#include <sys/time.h>
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(&t_ini, NULL);
cout << "Connecting to zeroMQ python server" << 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 (&reply);
string replyMessage = string(static_cast<char *>(reply.data()), reply.size());
cout << "Getting reply form zeroMQ python server: " + replyMessage << " " << endl;
gettimeofday(&t_fin, NULL);
printf("Tiempo en microsegundos: %ld microsegundosn",
((t_fin.tv_sec - t_ini.tv_sec)*1000000L
+t_fin.tv_usec) -t_ini.tv_usec
);
return 0;
}
此C 客户端代码的作品
我的目标是重写以前的代码,该代码将接口与使用C 标头文件进行实现。
我有client.h
文件,其中我定义了class Client
#ifndef ZEROMQ_CLASS_CLIENT_H
#define ZEROMQ_CLASS_CLIENT_H
#include <sys/time.h>
#include <iostream>
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
我有client.cpp
文件,其中我开发了我的setupConnection()
成员函数
#include "client.h"
#include <zmq.hpp>
using std::cout;
using std::endl;
Client::Client(){
};
void Client::setupConnection(){
gettimeofday(&t_before, NULL);
cout << "Connecting to python zeroMQ server ..."<<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(&reply);
replyMessage = string(static_cast<char *>(reply.data()), reply.size());
cout << "Getting response from server: " + replyMessage << " " << endl;
gettimeofday(&t_after, NULL);
printf("Time in microseconds: %ld microsecondsn",
((t_after.tv_sec - t_before.tv_sec)*1000000L
+t_before.tv_usec) -t_before.tv_usec
);
}
和main.cpp
文件中,我创建对象并致电该函数:
#include "client.h"
int main() {
Client c;
c.setupConnection();
return 0;
}
构建main.cpp
文件时,我会收到此消息:
% 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
我无法弄清楚我的代码有什么问题,有什么想法吗?
当前,您不编译client.cpp
。包括client.hpp
还不够(显然这是一个单独的文件,您的编译器不假定有相应的.cpp
文件。)尝试将client.cpp
添加到您的GCC命令,即g++ main.cpp client.cpp -lzmq
您不编译客户端:
g main.cpp client.cpp -o client.out -lzmq
相关文章:
- 如何从实现接口的模板化类实例访问结构
- 如何在C++中实现接口
- unique_ptr实现接口时对已删除函数的引用
- 避免对实现接口的类使用多个几乎相同的声明
- C#COM组装实现C 接口
- 当传递对类的引用时,优于另一个函数,即实现接口
- 通过其方法的子集实现接口
- C ++类实现接口,接口具有采用任何实现该接口的类的方法
- 错误:在 C++ 中实现接口模式时未定义对 typeinfo 的引用
- 与模板实现接口的类模板
- 在主应用程序中声明的 dll 中实现接口 - C++
- Objective-C类实现C 接口
- 如何添加实现C 接口的标签
- 强制接收实现接口的超类指针作为方法的参数
- 我可以部分实现接口层次结构吗
- 是否可以通过继承实现接口
- 在c++中实现接口
- 如何使用谷歌测试测试实现接口的不同类/结构
- 使用成员函数模板实现接口的功能
- C++ 11 实现接口的方法不可用。为什么?