【问题标题】:Why I receive error while compiling RapidJSON为什么我在编译 RapidJSON 时收到错误
【发布时间】:2020-11-14 23:14:03
【问题描述】:

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

【问题讨论】:

  • 您的问题令人困惑。声称您的问题是“为什么我收到解析 JSON 的错误”表明您成功编译了解析 JSON 的内容,但它在运行时遇到了某种问题。然而,上面显示了一堆编译错误,这意味着没有任何东西被编译、执行或解析任何东西,无论是 JSON 还是没有。如果您询问此编译错误,您能否准确解释您认为此 header 对象是什么,显示的代码需要像 protocolVersion 和其他成员这样导致这些错误的成员?
  • @SamVarshavchik,我应该显示多少代码才能避免混淆?代码说这些变量不是结构的成员,但是,如输入 JSON 中所示,它们存在
  • Stackoverflow 列出了minimal reproducible example 的要求。此外,输入 JSON 完全没有意义。您是否希望 RapidJSON 返回一个其成员具有来自 JSON 文件的名称的类?那是不可能的。 C++ 在其核心基础层面上不是这样工作的。您一定误解了文档中的某些内容。您能否发布一个链接,声称parse() 返回的rapidjson::GenericValue&lt;rapidjson::UTF8&lt;&gt; &gt; 具有任何这些字段?
  • @SamVarshavchik,parse() 返回一个 JSON 字符串,但是,我想获取所有变量值。据我所知,这应该是获取所有必需值的方法。单独来说,我可以使用 printf 获取值
  • 我确信获取所有变量值是有难度的。但是有必要研究图书馆的文档以确定如何做到这一点。尝试用 C++ 编写随机代码然后查看它是否有效不太可能完成任何事情。特别是当尝试违反 C++ 的基本规则时。在 C++ 中,不可能读取某种文件并总是返回一个类,其成员的名称总是与文件中的数据相同。 C++ 不能以这种方式工作。

标签: c++ json parsing rapidjson


【解决方案1】:

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;

【讨论】:

  • 我得到这个错误:错误:expected ‘;’ before ‘header’ 388 | header header; | ^~~~~~~ | ; 然而,它写在header header;之后const
  • 这是一个例子。你应该用你自己的结构来代替它。
  • 那么,我是否应该插入整个JSON结构并创建一个完整的结构类型作为示例?
  • 没有。您是否尝试使用上一个问题ItsPduHeader_t 中的对象类型而不是MessageHeader
  • 是的,我使用的是 header 而不是 MessageHeader。如何与多级结构一起工作?像这样:struct MessageCam { int X; struct MessageCamParameters { struct MessageBasicContainer { int X; struct MessageReferencePosition { int X; int X; struct MessagePositionConfidenceEllipse { int X; int X; int X; }; struct MessageAltitude { int X; int X; }; }; }; ...
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-03-13
  • 2018-01-22
  • 1970-01-01
  • 2023-03-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多