如何在Zeromq Publisher C 中修复运行时错误

How to fix a run-time error in ZeroMQ publisher c++

本文关键字:运行时错误 Publisher Zeromq      更新时间:2023-10-16

我试图使用C 在Zeromq中开发出发布者订户模型,在其中我从JSON文件中提取对象值并将其发送到另一侧。

我的订户零件在给出任何错误时运行良好。但是我在发布者部分面临以下错误:(在if语句中)

    src/lib_json/json_value.cpp:1136: Json::Value& 
    Json::Value::resolveReference(const char*, bool): Assertion `type_ == 
    nullValue || type_ == objectValue' failed.
    Aborted (core dumped)

这是我针对发布者的代码:

    #include "jsoncpp/include/json/value.h"
    #include "jsoncpp/include/json/reader.h"
    #include <fstream>
    #include "cppzmq/zmq.hpp"
    #include <string>
    #include <iostream>
    #include <unistd.h>
    using namespace std;
    int main () {
      zmq::context_t context(1);
      zmq::socket_t publisher (context, ZMQ_PUB);
      int sndhwm = 0;
      publisher.setsockopt (ZMQ_SNDHWM, &sndhwm, sizeof (sndhwm));
      publisher.bind("tcp://*:5561");
      const Json::Value p;
      ifstream pub_file("counter.json");
      Json::Reader reader;
      Json::Value root;
      if(pub_file != NULL && reader.parse(pub_file, root)) {
      const Json::Value p = root ["body"]["device_data"]["device_status"];
       }
      string text = p.asString();
      zmq::message_t message(text.size());
      memcpy(message.data() , text.c_str() , text.size());
      zmq_sleep (1);
      publisher.send(message);
      return 0;
     }
const Json::Value p;
...
if(pub_file != NULL && reader.parse(pub_file, root)) {
    const Json::Value p = root ["body"]["device_data"]["device_status"];
}
string text = p.asString();

创建 p if 语句中时,此新声明的变量仅适用于条件{} -code-block的范围。将其更改为已声明的变量 p

p = root ["body"]["device_data"]["device_status"];

这会更改外部范围中的变量,而不是在内部范围内声明一个新变量。另外,您应该将变量const Json::Value p标记为 const,因此您可以在条件中进行修改。