samedi 22 juillet 2017

C++: boost ptree remove children: no matching function

In an attempt to remove children of a boost property tree, I use a direct node in erase function which lead to

error: no matching function for call to 
boost::property_tree::basic_ptree<std::__cxx11::basic_string<char>

at

pt0.erase(pt_child);

What is the correct form of the code?

#include <iostream>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>

using namespace boost::property_tree;

void print(const ptree &p)
{
    std::stringstream ss;
    json_parser::write_json(ss, p);
    std::cout << ss.str() << std::endl;
}

int main()
{
    ptree pt0;

    for(int i = 0; i < 10; i++)
        pt0.put_child("kid" + std::to_string(i + 1), ptree());
    print(pt0);

    for(auto& pt_child : pt0)
        pt0.erase(pt_child);
    print(pt0);

    return 0;
}

Aucun commentaire:

Enregistrer un commentaire