问题
Using RapidJSON for parsing a JSON file, I get these errors.
This is part of the JSON file:
{
"header":{
"protocolVersion":2,
"messageID":2,
"stationID":224
},
"cam":{
"generationDeltaTime":37909,
"camParameters":{
"basicContainer":{
"stationType":5,
This is the code
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"];
I get this error. I don't know what it says they have no member.
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();
回答1:
header
is object of rapidjson::Value
type and doesn't have protocolVersion
, messageID
and stationID
members. You should provide your custom object type to store values from header
. The same goes for other variables (cam
and referencePosition
). For example:
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;
来源:https://stackoverflow.com/questions/64839622/why-i-receive-error-while-compiling-rapidjson