我试图使用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();
当您在语句内部创建另一个 变量时,此新声明的变量仅在条件{} -code-block范围内是局部的。将其更改为对已声明的变量的干净分配:p
if
p
p = root ["body"]["device_data"]["device_status"];
这会更改外部作用域中的变量,而不会在内部作用域中声明新的变量。另外,您应该将变量标记const Json::Value p
为not const
,以便可以在条件中对其进行修改。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句