如何在Zeromq Publisher C 中修复运行时错误
How to fix a run-time error in ZeroMQ publisher c++
我试图使用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
,因此您可以在条件中进行修改。
相关文章:
- 删除指向指针的指针是运行时错误吗
- c++中的指针和运行时错误
- 无法理解此 return 语句的功能,没有它就会发生运行时错误
- 在同一模拟中使用静脉和静脉_ inet内容时出现运行时错误
- 对单向链表进行排序时出现运行时错误
- 为什么此代码存在运行时错误?
- 你能解释一下什么运行时错误是如何解决它的吗?
- 为什么会出现 gettnig 运行时错误:加载类型为"_Bit_type"(stl_bvector.h) 的空指针?
- 为什么程序在 c++ 中迭代 emtpy 向量时会抛出运行时错误
- 运行时错误:引用绑定到类型为"int"的空指针
- 为什么当 vector 为空时会显示运行时错误?
- C++运行时错误与快速排序算法抛出堆栈转储错误
- 运行时错误:矢量下标超出范围:正在检查空集
- 分配给gslice_array会导致运行时错误
- cout 新创建的对象引发运行时错误
- C++在使用std::multimap时出现运行时错误的几率很小
- 运行时错误:引用绑定到类型"int"的未对齐地址0xbebebebebebebec6,这需要 4 个字节对齐 (stl_vector.h)
- 试图找出为什么我会收到运行时错误?
- 迭代二维矢量时发生运行时错误
- 如何在Zeromq Publisher C 中修复运行时错误