terminate called after throwing an instance of 'YAML::InvalidNode'
what(): invalid node; this may result from using a map iterator as a sequence iterator, or vice-versa
GDB跳入
yaml-cpp/node/imlp.h
。
this
是一个空节点,
m_isValid
是
false
。
#include <assert.h>
#include <fstream>
#include <iostream>
#include <map>
#include <yaml-cpp/yaml.h>
using namespace std;
int main() {
YAML::Node camerafile = YAML::LoadFile("./camera.yaml");
YAML::Node camera = camerafile["camera"];
auto response_values_yaml = camera["response_values"];
// it is the sequence iterator
for (YAML::const_iterator it = response_values_yaml.begin();
it != response_values_yaml.end(); ++it) {
YAML::Node response_values_map
= it->as<YAML::Node>(); // e.g. [0.0: 0.0, 1.0: 1.1, 245.0: 250.1]
for (YAML::const_iterator at = response_values_map.begin();
at != response_values_map.end(); ++at) {
// `at` is a **map iterater** which has a single key/value pair.
// so it will certainly coredump when you are calling `at->first.as<float>()`
assert(at->size() == 1);
// The iterator points to the single key/value pair.
YAML::Node::const_iterator sub_it = at->begin(); // **The key point.**
// Then access the key and value.
float key = sub_it->first.as<float>();
float value = sub_it->second.as<float>();
std::cout << " key: " << key << ", val: " << value << '\n';
}