YAML-cpp 解析嵌套映射和序列错误
yaml-cpp parsing nested maps and sequences error
我正在尝试解析一个带有嵌套映射和序列的文件,它有点像这样
annotations:
- dodge:
type: range based
attributes:
start_frame:
frame_number: 25
time_stamp: 2017-10-14 21:59:43
endframe:
frame_number: 39
time_stamp: 2017-10-14 21:59:45
distances:
- 2
- 5
- 6
我收到一条错误消息,指出确保节点存在。下面是我的示例代码。
YAML::Node basenode = YAML::LoadFile(filePath);
const YAML::Node& annotations = basenode["annotations"];
RangeBasedType rm;
for (YAML::const_iterator it = annotations.begin(); it != annotations.end(); ++it)
{
const YAML::Node& gestures = *it;
std::string type = gestures["type"].as<std::string>(); // this works
rm.setGestureName(type);
if (type == "range based")
{
const YAML::Node& attributes = gestures["attributes"];
for (YAML::const_iterator ti = attributes.begin(); ti != attributes.end(); ++ti)
{
const YAML::Node& frame = *ti;
if (frame["start_frame"]) // this fails saying it is not a valid node
{
std::cout << frame["frame_number"].as<int>();
rm.setStartFrame(frame["frame_number"].as<int>());
}
}
}
}
我希望从节点start_frame和end_frame获取frame_number。我已经检查了 YAML 格式的有效性。为什么这不起作用的任何原因?
这个循环:
for (YAML::const_iterator ti = attributes.begin(); ti != attributes.end(); ++ti)
正在迭代映射节点。因此,迭代器指向键/值对。您的下一行:
const YAML::Node& frame = *ti;
取消将其作为节点引用。相反,您需要查看其键/值节点:
const YAML::Node& key = ti->first;
const YAML::Node& value = ti->second;
yaml-cpp
允许迭代器指向节点和键/值对,因为它可以是映射或序列(或标量(,并且它是作为单个C++类型实现的。
相关文章:
- C++映射分割错误(核心转储)
- 错误处理.将系统错误代码映射到泛型
- 删除映射和分割错误中的一个过去结束元素
- 错误 C2760:语法错误:映射迭代器上意外的标记"标识符",预期的";"
- OpenGL 4.3 错误地将第 4 个纹理坐标映射到与第 3 个纹理坐标相同的位置
- 线程时访问静态映射时出现隔离错误
- C++ 将元素分配给映射值时访问错误
- C++映射容器 erase() 分段错误
- 为C++中的无序映射获取给定输入键的错误值
- std::映射服装比较函数和函数/lambda错误
- 自定义哈希表实现-将字符串映射到整数时出现内存错误
- 使用无序映射进行错误索引
- std::映射导致插入时出现C2664错误
- 与映射和unordered_map相关的编译错误:"attempting to reference a deleted function"
- 在共享内存中插入映射映射时出现编译器错误
- 访问range_expression中的嵌套元素会返回不完整的映射(段错误)
- std::map 擦除 - 将迭代器传递给错误的映射
- 解析序列/映射节点时出现无效的 yaml 节点错误
- 为什么 du -sh 输出错误大小的内存映射文件
- C ++尝试访问映射中的元素会给我一个不匹配的函数调用错误