I am writing a function that will load and save values into a .yaml
file (depending on what the toggle
input is; 'S' for save and 'L' for load). For some reason I am getting a Invalid initialization of non-const reference of type 'YAML::Node&' from an rvalue of type 'YAML::Node'
error in the line:
YAML::Node& child = parent[*currentToken];
The solution I found online was to make it constant by adding the const
keyword. However, like I said earlier, I need to be able to save value as well. Thus, I must be able to modify the node and cannot make it const. I am a bit lost as to what I should do now. My code takes in an iterator for the first node in the .yaml
file and the last node. Here is my code:
bool ReadParameter(YAML::Node& parent,
boost::tokenizer< boost::char_separator< char > >::iterator& currentToken,
const boost::tokenizer< boost::char_separator< char > >::iterator& lastToken, float& value,
char toggle) {
/*
* When we reach the last token, the distance between the iterators is 1. At this point we must
* check to see if the token node exists in the yaml file. If it does store the value associated
* with the token key in the variable `value` to load or store the value back into the yaml file to save.
*/
if (distance(currentToken, lastToken) == 1) {
if (parent[*(currentToken)].IsScalar()) {
if (toggle == 'L') { // Load
value = parent[*(currentToken)].as< float >();
}
else if (toggle == 'S') { // Save
parent[*(currentToken)] = value;
}
return true;
} else {
printf("Key %s does not give a scalar value.\n", (*currentToken).c_str());
return false;
}
}
// If the node is a map, get it's child. Else, our path does not exist.
if (parent.IsMap()) {
YAML::Node& child = parent[*currentToken];
// If there is no child node, return false
if (child.IsNull()) {
printf("There are no more child nodes.\n");
return false;
}
// Iterate to the next token.
return ReadParameter(child, ++currentToken, lastToken, value, toggle);
} else {
printf("Too many parameters, the parameter path is incorrect.\n");
return false;
}
}