2017-02-25 121 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中正常工作。