I was trying to develop a publisher subscriber model in ZeroMQ, using c++, where I am extracting an object value from a JSON-file and sending it to the other side.
我试图在ZeroMQ中开发一个发布者订阅器模型,使用c++,从json文件中提取一个对象值并将其发送到另一端。
My subscriber part is running well with giving any error. But I am facing the below error in the publisher part: ( in the if statement )
我的订户部分运行良好,没有任何错误。但我在发布者部分(在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)
This is my code for publisher:
这是我的出版商代码:
#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;
}
1 个解决方案
#1
1
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();
When you create another p
inside the if
statement, this newly declared variable is local just to the scope of the conditional {}-code-block. Change this to a clean assignment to an already declared variable p
:
当您在if语句中创建另一个p时,这个新声明的变量仅在条件{}-code-block的范围内是局部的。将其更改为已声明变量p的清洁赋值:
p = root ["body"]["device_data"]["device_status"];
This changes the variable in the outer scope, not declaring a new one inside the inner scope. Also, you should mark the variable const Json::Value p
as not const
, so you can modify it inside the conditional.
这将更改外部范围中的变量,而不是在内部范围内声明一个新的变量。此外,您应该将变量const Json::Value p标记为not const,以便您可以在条件语句中修改它。
#1
1
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();
When you create another p
inside the if
statement, this newly declared variable is local just to the scope of the conditional {}-code-block. Change this to a clean assignment to an already declared variable p
:
当您在if语句中创建另一个p时,这个新声明的变量仅在条件{}-code-block的范围内是局部的。将其更改为已声明变量p的清洁赋值:
p = root ["body"]["device_data"]["device_status"];
This changes the variable in the outer scope, not declaring a new one inside the inner scope. Also, you should mark the variable const Json::Value p
as not const
, so you can modify it inside the conditional.
这将更改外部范围中的变量,而不是在内部范围内声明一个新的变量。此外,您应该将变量const Json::Value p标记为not const,以便您可以在条件语句中修改它。