Где возникает ошибка при синтаксическом анализе RapidJSON

#c #json #struct #document #rapidjson

#c #json #структура #документ #rapidjson

Вопрос:

Я пытаюсь проанализировать файл JSON с помощью RapidJSON, я создал этот код для получения целых чисел (в данном случае), однако я получаю ошибки, говорящие о том, что некоторые переменные не являются членами структуры, когда они включены. Объявленная структура является исходным форматом JSON, но отформатирована как struct для получения значений.

        struct MessageHeader 
       {
          int protocolVersion;
          int messageID;
          int stationID;
       };
       struct MessageCam
       {
          int generationDeltaTime;
          struct MessageCamParameters
          {
         struct MessageBasicContainer
         {
            int stationType;
            struct MessageReferencePosition
            {
               int latitude;
               int longitude;
               struct MessagePositionConfidenceEllipse
               {
                  int semiMajorConfidence;
                  int semiMinorConfidence;
                  int semiMajorOrientation;
               };
               struct MessageAltitude
               {
                  int altitudeValue;
                  int altitudeConfidence;
               };
            };
         };
         struct MessageHighFrequencyContainer
         {
            struct MessageBasicVehicleContainerHighFrequency
            {
               struct MessageHeading
               {
                  int headingValue;
                  int headingConfidence;
               };
               struct MessageSpeed
               {
                  int speedValue;
                  int speedConfidence;
               };
               int driveDirection;
               struct MessageVehicleLength
               {
                  int vehicleLengthValue;
                  int vehicleLengthConfidenceIndication;
               };
               int vehicleWidth;
               struct MessageLongitudinalAcceleration
               {
                  int longitudinalAccelerationValue;
                  int longitudinalAccelerationConfidence;
               };
               struct MessageCurvature
               {
                  int curvatureValue;
                  int curvatureConfidence;
               };
               int curvatureCalculationMode;
               struct MessageYawRate
               {
                  int yawRateValue;
                  int yawRateConfidence;
               };
            };
         };
       };
    };
    
    
    vanetza::asn1::Cam message;     
    doc.Parse(pr);   
    
   
    const autoamp; header = doc["header"];
    MessageHeader messageHeader;
        
    messageHeader.protocolVersion = header["protocolVersion"].GetInt();   
    messageHeader.messageID = header["messageID"].GetInt(); 
    messageHeader.stationID = header["stationID"].GetInt(); 
        
    const autoamp; cam = doc["cam"];
    MessageCam messageCam;          
    messageCam.generationDeltaTime = cam["generationDeltaTime"].GetInt();     
    
    const autoamp; camParameters = cam["camParameters"];
    MessageCamParameters messageCamParameters;
    const autoamp; basicContainer = camParameters["basicContainer"];
    MessageBasicContainer messageBasicContainer;
    messageBasicContainer.stationType = basicContainer["stationType"].GetInt();
     
    const autoamp; referencePosition = basicContainer["referencePosition"];
    MessageReferencePosition messageReferencePosition;
    messageReferencePosition.latitude = referencePosition["latitude"].GetInt();                    
    messageReferencePosition.longitude = referencePosition["longitude"].GetInt();
    

    const autoamp; positionConfidenceEllipse = referencePosition["positionConfidenceEllipse"];
    MessagePositionConfidenceEllipse messagePositionConfidenceEllipse;
    messagePositionConfidenceEllipse.semiMajorConfidence = positionConfidenceEllipse["semiMajorConfidence"].GetInt();
    messagePositionConfidenceEllipse.semiMinorConfidence = positionConfidenceEllipse["semiMinorConfidence"].GetInt();
    messagePositionConfidenceEllipse.semiMajorOrientation = positionConfidenceEllipse["semiMajorOrientation"].GetInt();


    const autoamp; altitude = basicContainer["altitude"];
    MessageAltitude messageAltitude;
    messageAltitude.altitudeValue = altitude["altitudeValue"].GetInt();
   
messageAltitude.altitudeConfidence = altitude["altitudeConfidence"].GetInt();
    


    
    
    const autoamp; highFrequencyContainer = camParameters["highFrequencyContainer"];
    MessageHighFrequencyContainer messageHighFrequencyContainer;
    const autoamp; basicVehicleContainerHighFrequency = highFrequencyContainer["basicVehicleContainerHighFrequency"];
    MessageBasicVehicleContainerHighFrequency messageBasicVehicleContainerHighFrequency;
    const autoamp; heading = basicVehicleContainerHighFrequency["heading"];
    MessageHeading messageHeading;
    messageHeading.headingValue = heading["headingValue"].GetInt();
    messageHeading.headingConfidence = heading["heading"]["headingConfidence"].GetInt();
    const autoamp; speed = basicVehicleContainerHighFrequency["speed"];
    MessageSpeed messageSpeed;
    messageSpeed.speedValue = speed["speedValue"].GetInt();
    messageSpeed.speedConfidence = speed["speedConfidence"].GetInt();

    messageBasicVehicleContainerHighFrequency.driveDirection = basicVehicleContainerHighFrequency["driveDirection"].GetInt();

    const autoamp; vehicleLength = basicVehicleContainerHighFrequency["vehicleLength"];
    MessageVehicleLength messageVehicleLength;
    messageVehicleLength.vehicleLengthValue = vehicleLength["vehicleLengthValue"].GetInt();
    messageVehicleLength.vehicleLengthConfidenceIndication = vehicleLength["vehicleLengthConfidenceIndication"].GetInt();

    messageBasicVehicleContainerHighFrequency.vehicleWidth = basicVehicleContainerHighFrequency["vehicleWidth"].GetInt();

    const autoamp; longitudinalAcceleration = basicVehicleContainerHighFrequency["longitudinalAcceleration"];
    MessageLongitudinalAcceleration messageLongitudinalAcceleration;
    messageLongitudinalAcceleration.longitudinalAccelerationValue = longitudinalAcceleration["longitudinalAccelerationValue"].GetInt();
    messageLongitudinalAcceleration.longitudinalAccelerationConfidence = longitudinalAcceleration["longitudinalAccelerationConfidence"].GetInt();

     const autoamp; curvature = basicVehicleContainerHighFrequency["curvature"]; 
     MessageCurvature messageCurvature;
    messageCurvature.curvatureValue = curvature["curvatureValue"].GetInt();
    messageCurvature.curvatureConfidence = curvature["curvatureConfidence"].GetInt();

    messageBasicVehicleContainerHighFrequency.curvatureCalculationMode = basicVehicleContainerHighFrequency["curvatureCalculationMode"].GetInt();

    const autoamp; yawRate = basicVehicleContainerHighFrequency["yawRate"];
    MessageYawRate messageYawRate;
    messageYawRate.yawRateValue = yawRate["yawRateValue"].GetInt();
    messageYawRate.yawRateConfidence = yawRate["yawRateConfidence"].GetInt();
  

Тем не менее, я получаю эти ошибки:

 cpp:479:5: error: ‘MessageCamParameters’ was not declared in this scope; did you mean ‘CamParameters’?
  479 |     MessageCamParameters messageCamParameters;
      |     ^~~~~~~~~~~~~~~~~~~~
      |     CamParameters
cpp:481:5: error: ‘MessageBasicContainer’ was not declared in this scope; did you mean ‘BasicContainer’?
  481 |     MessageBasicContainer messageBasicContainer;
      |     ^~~~~~~~~~~~~~~~~~~~~
      |     BasicContainer
cpp:482:5: error: ‘messageBasicContainer’ was not declared in this scope; did you mean ‘BasicContainer’?
  482 |     messageBasicContainer.stationType = basicContainer["stationType"].GetInt();
      |     ^~~~~~~~~~~~~~~~~~~~~
      |     BasicContainer
cpp:485:5: error: ‘MessageReferencePosition’ was not declared in this scope; did you mean ‘DeltaReferencePosition’?
  485 |     MessageReferencePosition messageReferencePosition;
      |     ^~~~~~~~~~~~~~~~~~~~~~~~
      |     DeltaReferencePosition
cpp:486:5: error: ‘messageReferencePosition’ was not declared in this scope; did you mean ‘DeltaReferencePosition’?
  486 |     messageReferencePosition.latitude = referencePosition["latitude"].GetInt();
      |     ^~~~~~~~~~~~~~~~~~~~~~~~
      |     DeltaReferencePosition
cpp:491:5: error: ‘MessagePositionConfidenceEllipse’ was not declared in this scope; did you mean ‘positionConfidenceEllipse’?
  491 |     MessagePositionConfidenceEllipse messagePositionConfidenceEllipse;
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |     positionConfidenceEllipse
cpp:492:5: error: ‘messagePositionConfidenceEllipse’ was not declared in this scope; did you mean ‘positionConfidenceEllipse’?
  492 |     messagePositionConfidenceEllipse.semiMajorConfidence = positionConfidenceEllipse["semiMajorConfidence"].GetInt();
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |     positionConfidenceEllipse
cpp:498:5: error: ‘MessageAltitude’ was not declared in this scope; did you mean ‘DeltaAltitude’?
  498 |     MessageAltitude messageAltitude;
      |     ^~~~~~~~~~~~~~~
      |     DeltaAltitude
cpp:499:5: error: ‘messageAltitude’ was not declared in this scope; did you mean ‘DeltaAltitude’?
  499 |     messageAltitude.altitudeValue = altitude["altitudeValue"].GetInt();
      |     ^~~~~~~~~~~~~~~
      |     DeltaAltitude
cpp:509:5: error: ‘MessageHighFrequencyContainer’ was not declared in this scope; did you mean ‘HighFrequencyContainer’?
  509 |     MessageHighFrequencyContainer messageHighFrequencyContainer;
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |     HighFrequencyContainer
cpp:511:5: error: ‘MessageBasicVehicleContainerHighFrequency’ was not declared in this scope; did you mean ‘BasicVehicleContainerHighFrequency’?
  511 |     MessageBasicVehicleContainerHighFrequency messageBasicVehicleContainerHighFrequency;
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |     BasicVehicleContainerHighFrequency
cpp:513:5: error: ‘MessageHeading’ was not declared in this scope; did you mean ‘MessageHeader’?
  513 |     MessageHeading messageHeading;
      |     ^~~~~~~~~~~~~~
      |     MessageHeader
cpp:514:5: error: ‘messageHeading’ was not declared in this scope; did you mean ‘messageHeader’?
  514 |     messageHeading.headingValue = heading["headingValue"].GetInt();
      |     ^~~~~~~~~~~~~~
      |     messageHeader
cpp:517:5: error: ‘MessageSpeed’ was not declared in this scope
  517 |     MessageSpeed messageSpeed;
      |     ^~~~~~~~~~~~
cpp:518:5: error: ‘messageSpeed’ was not declared in this scope
  518 |     messageSpeed.speedValue = speed["speedValue"].GetInt();
      |     ^~~~~~~~~~~~
cpp:521:5: error: ‘messageBasicVehicleContainerHighFrequency’ was not declared in this scope; did you mean ‘BasicVehicleContainerHighFrequency’?
  521 |     messageBasicVehicleContainerHighFrequency.driveDirection = basicVehicleContainerHighFrequency["driveDirection"].GetInt();
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |     BasicVehicleContainerHighFrequency
cpp:524:5: error: ‘MessageVehicleLength’ was not declared in this scope; did you mean ‘VehicleLength’?
  524 |     MessageVehicleLength messageVehicleLength;
      |     ^~~~~~~~~~~~~~~~~~~~
      |     VehicleLength
cpp:525:5: error: ‘messageVehicleLength’ was not declared in this scope; did you mean ‘VehicleLength’?
  525 |     messageVehicleLength.vehicleLengthValue = vehicleLength["vehicleLengthValue"].GetInt();
      |     ^~~~~~~~~~~~~~~~~~~~
      |     VehicleLength
cpp:531:5: error: ‘MessageLongitudinalAcceleration’ was not declared in this scope; did you mean ‘LongitudinalAcceleration’?
  531 |     MessageLongitudinalAcceleration messageLongitudinalAcceleration;
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |     LongitudinalAcceleration
cpp:532:5: error: ‘messageLongitudinalAcceleration’ was not declared in this scope; did you mean ‘LongitudinalAcceleration’?
  532 |     messageLongitudinalAcceleration.longitudinalAccelerationValue = longitudinalAcceleration["longitudinalAccelerationValue"].GetInt();
      |     ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      |     LongitudinalAcceleration
cpp:536:6: error: ‘MessageCurvature’ was not declared in this scope
  536 |      MessageCurvature messageCurvature;
      |      ^~~~~~~~~~~~~~~~
cpp:537:5: error: ‘messageCurvature’ was not declared in this scope
  537 |     messageCurvature.curvatureValue = curvature["curvatureValue"].GetInt();
      |     ^~~~~~~~~~~~~~~~
cpp:543:5: error: ‘MessageYawRate’ was not declared in this scope
  543 |     MessageYawRate messageYawRate;
      |     ^~~~~~~~~~~~~~
cpp:544:5: error: ‘messageYawRate’ was not declared in this scope
  544 |     messageYawRate.yawRateValue = yawRate["yawRateValue"].GetInt();
      |     ^~~~~~~~~~~~~~
  

Есть ли проблема с определением структуры?

Комментарии:

1. Привет, добро пожаловать в SO! В вашем вопросе есть стена кода, и я совершенно уверен, что большая его часть не нужна. Не могли бы вы внести изменения в свой пост, чтобы включить только минимально возможный объем кода, необходимый для воспроизведения проблемы?

Ответ №1:

    struct MessageCam
   {
      int generationDeltaTime;
      struct MessageCamParameters
      {
  

MessageCamParameters объявляется как член MessageCam . Это то, что происходит в C , когда вы объявляете класс внутри другого класса.

 MessageCamParameters messageCamParameters;
  

Это попытка определить объект типа MessageCamParameters . Вы не объявили класс или вызываемую структуру MessageCamParameters . Вы объявили класс с таким именем как член другого класса. Итак, в этом случае, чтобы правильно ссылаться на него, это должно быть:

 MessageCam::MessageCamParameters messageCamParameters;
  

Если вы не хотите объявлять этот класс как член другого класса, чтобы он был простым, садовым MessageCamParameters , тогда, что ж, не делайте этого. Не объявляйте класс как член другого класса.

Комментарии:

1. Итак, могу ли я избежать объявления класса, в котором нет никакой переменной внутри него напрямую? Я имею в виду, например, stationType, должен ли я сделать это так MessageCam::MessageCamParameters::MessageBasicContainer messageBasicContainer или непосредственно MessageBasicContainer messageBasicContainer в этом последнем случае, я создал структуру, struct MessageBasicContainer { int stationType; }; оставив предыдущие объявления

2. Если я вас правильно понимаю, любая альтернатива, конечно, будет работать. Какой бы подход вы ни выбрали, это полностью зависит от вас.