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

Why I receive error while compiling RapidJSON

使用 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;