为什么我在编译RapidJSON时收到错误

Why I receive error while compiling RapidJSON

本文关键字:错误 RapidJSON 编译 为什么      更新时间:2024-09-29

使用RapidJSON解析JSON文件时,我会收到这些错误。

这是JSON文件的一部分:

{
"header":{
"protocolVersion":2,
"messageID":2,
"stationID":224
},
"cam":{
"generationDeltaTime":37909,
"camParameters":{
"basicContainer":{
"stationType":5,

这是代码

doc.Parse(pr);   


const auto& header = doc["header"];
header.protocolVersion = doc["header"]["protocolVersion"].GetInt();   
header.messageID = doc["header"]["messageID"].GetInt(); 
header.stationID = doc["header"]["stationID"].GetInt(); 

const auto& cam = doc["cam"];


cam.camParameters.basicContainer.stationType = doc["cam"]["camParameters"]["basicContainer"]["stationType"].GetInt();

const auto& referencePosition = doc["cam"]["camParameters"]["basicContainer"]["referencePosition"];

我得到这个错误。我不知道上面怎么说他们没有会员。

In member function ‘void MqttApplication::sendm(const std::__cxx11::basic_string<char>&)’:
.cpp:389:12: error: ‘const class rapidjson::GenericValue<rapidjson::UTF8<> >’ has no member named ‘protocolVersion’
389 |     header.protocolVersion = doc["header"]["protocolVersion"].GetInt();
|            ^~~~~~~~~~~~~~~
mqtt_application.cpp:390:12: error: ‘const class rapidjson::GenericValue<rapidjson::UTF8<> >’ has no member named ‘messageID’
390 |     header.messageID = doc["header"]["messageID"].GetInt();
|            ^~~~~~~~~
mqtt_application.cpp:391:12: error: ‘const class rapidjson::GenericValue<rapidjson::UTF8<> >’ has no member named ‘stationID’
391 |     header.stationID = doc["header"]["stationID"].GetInt();
|            ^~~~~~~~~
mqtt_application.cpp:402:9: error: ‘const class rapidjson::GenericValue<rapidjson::UTF8<> >’ has no member named ‘generationDeltaTime’
402 |     cam.generationDeltaTime = doc["cam"]["generationDeltaTime"].GetInt();
|         ^~~~~~~~~~~~~~~~~~~
mqtt_application.cpp:405:9: error: ‘const class rapidjson::GenericValue<rapidjson::UTF8<> >’ has no member named ‘camParameters’
405 |     cam.camParameters.basicContainer.stationType = doc["cam"]["camParameters"]["basicContainer"]["stationType"].GetInt();

headerrapidjson::Value类型的对象,不具有protocolVersionmessageIDstationID成员。您应该提供自定义对象类型来存储header中的值。其他变量(camreferencePosition(也是如此。例如:

struct MessageHeader
{
int protocolVersion;
int messageID;
int stationID;
};
//...
const auto& header = doc["header"];
MessageHeader messageHeader;
messageHeader.protocolVersion = header["protocolVersion"].GetInt();
messageHeader.messageID = header["messageID"].GetInt();
messageHeader.stationID = header["stationID"].GetInt();
std::cout << "message header protocol version: " << messageHeader.protocolVersion << std::endl;