1

我正在为一个项目使用 Boost.PropertyTree,我想对 Key 和 Data 使用用户定义的类型,而不是 Boost 在 ptree typedef 中使用的 std::string。

但是,当我自己 typedef basic_ptree 时,会出现以下编译器错误:

1>  main.cpp
1>c:\boost_1_49_0\boost\property_tree\ptree.hpp(82): error C2027: use of undefined type 'boost::property_tree::path_of<Key>'
1>          with
1>          [
1>              Key=int
1>          ]
1>          c:\users\mathias\documents\visual studio 2012\projects\testappern\testappern\main.cpp(10) : see reference to class template instantiation 'boost::property_tree::basic_ptree<Key,Data>' being compiled
1>          with
1>          [
1>              Key=int,
1>              Data=int
1>          ]
1>c:\users\mathias\documents\visual studio 2012\projects\testappern\testappern\main.cpp(13): error C2664: 'boost::property_tree::basic_ptree<Key,Data>::add_child' : cannot convert parameter 1 from 'int' to 'const boost::type &'
1>          with
1>          [
1>              Key=int,
1>              Data=int
1>          ]
1>          Reason: cannot convert from 'int' to 'const boost::type'
1>          The target type has no constructors
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

以下代码片段显示了我如何 typedef basic_tree 并获取编译器错误的示例:

#include <iostream>
#include <boost\property_tree\ptree.hpp>

using namespace boost::property_tree;

typedef basic_ptree<int, int> IntTree;

int main(int argc, char* argv[])
{
    IntTree tree;
    IntTree child;
    int index = 42;
    tree.add_child(index, child);

    return 0;
}

所以我的问题是,我如何正确地键入它?如果有兴趣,我正在运行 MSVC 2012。

提前致谢!

4

2 回答 2

1

根据 的内容boost/property_tree/ptree_fwd.hpp,如果你想使用自定义类型作为键,你应该遵循这个:

/// If you want to use a custom key type, specialize this struct for it
/// and give it a 'type' typedef that specifies your path type. The path
/// type must conform to the Path concept described in the documentation.
/// This is already specialized for std::basic_string.

所以不能简单地使用int。如代码所述,请参阅文档以获取更多信息。

于 2012-08-20T10:44:29.823 回答
1

这是一个例子 basic_ptree <int, int>

//path class must conform Path concept
template<> class boost::property_tree::path_of < int >
{
public:
    typedef int key_type;
    typedef boost::property_tree::path_of < int > type;
    boost::property_tree::path_of<int>(vector<int>& vals)
    {
        std::transform(vals.begin(), vals.end(), back_inserter(_impl),  
            [&](int v) -> int{ return v; });
    }
    key_type reduce()   
    {
        key_type res = *_impl.begin();
        _impl.pop_front();
        return res;
    }
    bool empty() const
    {
        return _impl.empty();
    }
    bool single() const
    {
        return _impl.size() == 1;
    }
    std::string dump() const
    {
        std::string res;
        std::for_each(_impl.begin(),
        _impl.end(), [&res](key_type k) -> void
        {
            res.append(".");
            res.append(boost::lexical_cast<std::string>(k));
        });
        return res;
    }
    private:
         deque<key_type> _impl;
};
//usage:
typedef boost::property_tree::basic_ptree < int, int> custom_int_tree_t
//or 
typedef boost::property_tree::basic_ptree < int, void*> custom_int_tree2_t
于 2015-02-03T16:35:42.617 回答