gpt4 book ai didi

c++ - 尝试编译基本的 Boost 属性树示例时出现问题 - 编译错误。该怎么办?

转载 作者:行者123 更新时间:2023-11-28 03:48:18 26 4
gpt4 key购买 nike

所以 this is official Boost PropertyTree example ,我尝试在我的 visual studio 2008 上编译它,在 VS2010 上同样编译它得到 Error C2228: left of '.put_value' must have class/struct/union c:\program files (x86)\boost-1.46.1\include\boost\property_tree\detail\ptree_implementation.hpp 795 1

这是我使用的代码:

#include <stdio.h>
#include <iostream>
#include <fstream>
#include <set>
#include <sstream>
#include <boost/foreach.hpp>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>

using namespace std;

std::string m_file; // log filename
int m_level; // debug level
std::set<string> m_modules; // modules where logging is enabled

// Loads debug_settings structure from the specified XML file
void load( std::ifstream &file)
{
// Create an empty property tree object
using boost::property_tree::ptree;
ptree pt;

// Load the XML file into the property tree. If reading fails
// (cannot open file, parse error), an exception is thrown.
read_xml(file, pt);

// Get the filename and store it in the m_file variable.
// Note that we construct the path to the value by separating
// the individual keys with dots. If dots appear in the keys,
// a path type with a different separator can be used.
// If the debug.filename key is not found, an exception is thrown.
m_file = pt.get<std::string>("debug.filename");

// Get the debug level and store it in the m_level variable.
// This is another version of the get method: if the value is
// not found, the default value (specified by the second
// parameter) is returned instead. The type of the value
// extracted is determined by the type of the second parameter,
// so we can simply write get(...) instead of get<int>(...).
m_level = pt.get("debug.level", 0);

// Iterate over the debug.modules section and store all found
// modules in the m_modules set. The get_child() function
// returns a reference to the child at the specified path; if
// there is no such child, it throws. Property tree iterators
// are models of BidirectionalIterator.
BOOST_FOREACH(ptree::value_type &v,
pt.get_child("debug.modules"))
m_modules.insert(v.second.data());
}

// Saves the debug_settings structure to the specified XML file
void save(const std::string &filename)
{
// Create an empty property tree object
using boost::property_tree::ptree;
ptree pt;

// Put log filename in property tree
pt.put("debug.filename", m_file);

// Put debug level in property tree
pt.put("debug.level", m_level);

// Iterate over the modules in the set and put them in the
// property tree. Note that the put function places the new
// key at the end of the list of keys. This is fine most of
// the time. If you want to place an item at some other place
// (i.e. at the front or somewhere in the middle), this can
// be achieved using a combination of the insert and put_own
// functions.
BOOST_FOREACH(const std::string &name, m_modules)
pt.put("debug.modules.module", name, true);

// Write the property tree to the XML file.
write_xml(filename, pt);
}

int main()
{
std::ifstream script( "<debug>\n <filename>debug.log</filename>\n <modules>\n <module>Finance</module>\n <module>Admin</module>\n <module>HR</module>\n </modules>\n <level>2</level>\n</debug>\n");
load(script);
save("j.js");
cout << "parse complete" << endl;
cin.get();
}

我没有以任何方式更改我的 boost 发行版,但它无法编译。为什么?

最佳答案

1.46.1 的示例代码已更改:

http://www.boost.org/doc/libs/1_46_1/libs/property_tree/examples/debug_settings.cpp

相关的变化是

BOOST_FOREACH(const std::string &name, m_modules)pt.add("debug.modules.module", name);

关于c++ - 尝试编译基本的 Boost 属性树示例时出现问题 - 编译错误。该怎么办?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6645105/

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