c++ - 无法通过 ZMQ 发送 capnp 消息

标签 c++ arrays serialization zeromq capnproto

我有以下代码,我正在尝试通过 ZMQ 发送 capnp 消息:

::capnp::MallocMessageBuilder message;    
Guitar::Builder guitar = message.initRoot<Guitar>();  
guitar.setModel(1); 
guitar.setPrice(1200);

kj::Array<capnp::word> words = messageToFlatArray(message);

_publisher.send(words);

我正在使用发布-订阅模式,但出现以下错误:

error: no matching member function for call to  
      'send'  
        _publisher.send(words);  
        ~~~~~~~~~~~^~~~ /usr/local/include/zmq.hpp:610:21: note: candidate function not viable: no known conversion from  
'kj::Array<capnp::word>' to  
      'zmq::message_t &' for 1st argument  
        inline bool send (message_t &msg_, int flags_ = 0)  
                    ^ /usr/local/include/zmq.hpp:627:21: note: candidate   function not viable: no known conversion from
'kj::Array<capnp::word>' to
      'zmq::message_t' for 1st argument
        inline bool send (message_t &&msg_, int flags_ = 0)
                    ^ /usr/local/include/zmq.hpp:620:35: note: candidate function template not viable: requires at least 2 arguments,
but 1 was
      provided
        template<typename I    bool send(I first, I last, int flags_=0)
                                  ^ /usr/local/include/zmq.hpp:600:23: note: candidate function not viable: requires at least 2 arguments,
but 1 was provided
        inline size_t send (const void *buf_, size_t len_, int flags_ = 0)

最佳答案

kj::ArrayPtr 来自 KJ 库,它是 Cap'n Proto 的一部分。 ZeroMQ 对 KJ 一无所知,因此无法识别该类型。它要么需要一个 message_t(一种 ZeroMQ 类型),要么需要一个指针/大小组合。你可以这样做:

auto bytes = words.asBytes();
_publisher.send(bytes.begin(), bytes.size());

关于c++ - 无法通过 ZMQ 发送 capnp 消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38647671/

相关文章:

C++字符串如何

c++ - 求和未确定范围内的奇数和偶数

c++ - C++子数组与现有数组的偏移量

带有 OR 的 javascript 数组声明

java - 方法 toArray() 不适用于列表

php - jquery 序列化和 $.post 并在服务器端接收不完整的数组

c++ - 模板队列得到错误的结果

c++ - Median of Medians 算法误解的中位数?

perl - 我应该如何在两个 KiokuDB 目录之间复制对象?

c# - 在 C# 中以顺序方式使用 BinaryFormatter