我有以下代码,我正在尝试通过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来自K'库,属于Cap'n Proto。 ZeroMQ对KJ一无所知,因此不会识别该类型。它需要message_t(ZeroMQ类型)或指针/大小组合。你可以做:

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

09-08 11:38