Search code examples
c++jsonstructdocumentrapidjson

Where the error come while parsing RapidJSON


I'm trying to parse a JSON file using RapidJSON, I've created this code to get the integers (in this case), however, I get errors saying that some variables are not members of the structure when they are included. The declared struct is the original JSON format but formatted as struct to get the values.

       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 auto& header = doc["header"];
    MessageHeader messageHeader;
        
    messageHeader.protocolVersion = header["protocolVersion"].GetInt();   
    messageHeader.messageID = header["messageID"].GetInt(); 
    messageHeader.stationID = header["stationID"].GetInt(); 
        
    const auto& cam = doc["cam"];
    MessageCam messageCam;          
    messageCam.generationDeltaTime = cam["generationDeltaTime"].GetInt();     
    
    const auto& camParameters = cam["camParameters"];
    MessageCamParameters messageCamParameters;
    const auto& basicContainer = camParameters["basicContainer"];
    MessageBasicContainer messageBasicContainer;
    messageBasicContainer.stationType = basicContainer["stationType"].GetInt();
     
    const auto& referencePosition = basicContainer["referencePosition"];
    MessageReferencePosition messageReferencePosition;
    messageReferencePosition.latitude = referencePosition["latitude"].GetInt();                    
    messageReferencePosition.longitude = referencePosition["longitude"].GetInt();
    

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


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


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

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

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

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

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

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

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

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

However, I get these errors:

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();
      |     ^~~~~~~~~~~~~~

Is there a problem with the structure definition?


Solution

  •    struct MessageCam
       {
          int generationDeltaTime;
          struct MessageCamParameters
          {
    

    MessageCamParameters is declared as a member of MessageCam. That's what happens in C++ when you declare a class inside another class.

    MessageCamParameters messageCamParameters;
    

    This attempts to define an object of type MessageCamParameters. You did not declare a class or a structure called MessageCamParameters. You declared a class of that name as a member of another class. So, in this case, to properly reference it, this should be:

    MessageCam::MessageCamParameters messageCamParameters;
    

    If you did not wish to declare this class as a member of another class, so that it's a plain, garden-variety MessageCamParameters then, well, don't do that. Don't declare a class as a member of another class.