2017-02-25 91 views
1

无法从管理使用YAML-CPP库YAML节点中删除子节点。这是我想要的代码:删除节点,我似乎

YAML::Node y = YAML::Load("\ 
    a: first\n\ 
    b: second\n\ 
    c: \n\ 
     d: third\n\ 
     e: \n\ 
      f: fourth\n\ 
      g: fifth\n\ 
    "); 
    cout << y; 
    cout << endl << endl; 
    y.remove(y["b"]); 
    cout << y; 
    cout << endl; 

,这是输出:

a: first 
c: 
    e: 
    g: fifth 
    f: fourth 
    d: third 
b: second 

a: first 
c: 
    e: 
    g: fifth 
    f: fourth 
    d: third 
b: second 

而预期输出应该是与第二发射YAML流不包含在“B”元素。

缺少什么我在这里?谢谢:)

回答

1

原来,node.remove()在YAML-CPP 0.5.2,这是在Ubuntu Xenial和许多其他发行版的当前版本被打破。 (https://github.com/jbeder/yaml-cpp/issues/338

的代码应该是这样的:

YAML::Node y = YAML::Load("\ 
a: first\n\ 
b: second\n\ 
c: \n\ 
    d: third\n\ 
    e: \n\ 
     f: fourth\n\ 
     g: fifth\n\ 
"); 
cout << y; 
cout << endl << endl; 
y.remove("b"); 
cout << y; 
cout << endl; 

所以remove正确的参数是一个字符串(因为我从地图中删除),这原本不编译:

/usr/include/yaml-cpp/node/impl.h:392:58: required from ‘bool YAML::Node::remove(const Key&) [with Key = char [2]]’ 
/turbine/turbine/src/components/yaml-test.cpp:51:15: required from here 
/usr/include/yaml-cpp/node/detail/impl.h:136:15: error: ‘equals’ was not declared in this scope 
if (equals(*it->first, key, pMemory)) { 
... 

但在0.5.3中正常工作。