Boost Json Write error: no matching function for call to ‘boost::property_tree::basic_ptree<std::__cxx11

Boost Json Write error: no matching function for call to ‘boost::property_tree::basic_ptree<std::__cxx11

我正在尝试使用 Boost 库将 Json 数据写入字符串,但遇到编译错误:

error: no matching function for call to ‘boost::property_tree::basic_ptree<std::__cxx11::basic_string<char>, std::__cxx11::basic_string<char> >::push_back(std::pair<const char*, const char*>)

我的 C++ 代码是

#include <fstream>
#include <iostream>

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

namespace pt = boost::property_tree;

int main()
{
  std::string enc = "Enoded data";

    pt::ptree root;
    pt::write_json(std::cout, root);

    pt::ptree image_node;
    image_node.push_back(std::make_pair("content", enc));
    root.add_child("image", image_node);

    pt::ptree features_node;
    features_node.push_back(std::make_pair("type", "LABEL_DETECTION"));
    features_node.push_back(std::make_pair("maxResults", 1)); 
    root.add_child("features", features_node);
    pt::write_json(std::cout, root);
    return 0;
}

boost::property_tree::ptree::push_back接受一个boost::property_tree::ptree::value_type作为参数,与std::pair<const char*, const char*>不同。所以,你需要例如。 :

features_node.push_back(pt::ptree::value_type("type", pt::ptree("LABEL_DETECTION")));

或者更好的是,只需使用 boost::property_tree::ptree::put :

pt::ptree root;

root.put("image.content", enc);
root.put("features.type", "LABEL_DETECTION");
root.put("features.maxResults", 1);

pt::write_json(std::cout, root);