gpt4 book ai didi

c++ - Boost Json 写入错误:没有匹配函数来调用‘boost::property_tree::basic_ptree
转载 作者:行者123 更新时间:2023-11-28 04:35:12 24 4
gpt4 key购买 nike

我正在尝试使用 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);

关于c++ - Boost Json 写入错误:没有匹配函数来调用‘boost::property_tree::basic_ptree<std::__cxx11,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51655448/

24 4 0

Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com