【问题标题】:Typedef'ing basic_ptree from Boost来自 Boost 的 typedef'ing basic_ptree
【发布时间】:2012-08-20 10:34:35
【问题描述】:

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

但是,当我自己键入def 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 ==========

以下 sn-p 显示了我如何键入def 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;
}

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

提前致谢!

【问题讨论】:

  • 与您的问题完全无关,但如果您希望代码可移植,则不应在包含路径中使用反斜杠。 VC++ 已经能够处理正斜杠很长时间了。
  • @JoachimPileborg 感谢您提醒我。这只是我创建的一个快速 sn-p,当我懒得自己输入整个路径时,Visual Studio 的自动完成使用正斜杠。

标签: c++ templates boost compiler-errors boost-propertytree


【解决方案1】:

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

/// 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。如代码所示,请参阅文档以获取更多信息。

【讨论】:

【解决方案2】:

这里是basic_ptree &lt;int, int&gt;的例子

//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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-12
    • 1970-01-01
    • 1970-01-01
    • 2011-02-12
    相关资源
    最近更新 更多