Boost property_tree 用于存储指针

发布于 2024-12-09 19:21:04 字数 858 浏览 1 评论 0原文

是否可以在 boost 属性树中存储指向对象的指针,然后使用迭代器来检索数据?我正在尝试执行类似的操作:

property_tree::ptree pt;
pt.put<CGUICrateElement*>("1.2.3.4", new MyObject() );
//... more tree construction here...

然后递归地迭代所有树节点,例如:

property_tree::ptree::iterator iter = treeNode.begin();
property_tree::ptree::iterator iter_end = treeNode.end();

for ( ; iter != iter_end; ++iter )
{
MyObject *obj = lexical_cast<MyObject*>(iter->second.data());
    //... etc 

问题是我收到错误 lexical_cast.hpp:1112: error: no match for 'operator>>'在“流>>”中词法转换行上的输出'。

并将以下内容添加到 MyObject 并没有帮助

friend std::istream& operator>>(std::istream& in, MyObject& obj){ return in; }

我也尝试了 c 强制转换和动态强制转换,但无济于事。

在 ptree 中是否可以使用指针?我即将创建自己的树结构作为解决方法,我想我会先在这里问。

干杯。

Is it possible to store pointers to objects in boost property trees, and then use an iterator to retrieve the data? I'm trying to do something like:

property_tree::ptree pt;
pt.put<CGUICrateElement*>("1.2.3.4", new MyObject() );
//... more tree construction here...

and then recursively itererate through all the tree nodes with something like:

property_tree::ptree::iterator iter = treeNode.begin();
property_tree::ptree::iterator iter_end = treeNode.end();

for ( ; iter != iter_end; ++iter )
{
MyObject *obj = lexical_cast<MyObject*>(iter->second.data());
    //... etc 

The problem is I get the error lexical_cast.hpp:1112: error: no match for 'operator>>' in 'stream >> output' on the lexical cast line.

and adding the following to MyObject doesn't help

friend std::istream& operator>>(std::istream& in, MyObject& obj){ return in; }

I've also tried c casts and dynamic casts to no avail.

Is using pointers even possible inside a ptree? I'm about to just create my own tree structure as a workaround by I figured I'd ask here first.

Cheers.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

薄情伤 2024-12-16 19:21:04

添加运算符>>当您实际尝试 lexical_cast 到 MyObject 的指针时,对 MyObject 的引用将无济于事。您可以想象创建一个operator>>(std::istream&, MyObject*&)。但是,请记住,property_tree 是为从文本文件读取配置而设计的,因此您将享受在对象与文本之间进行转换的乐趣。

不要使用 property_tree 作为通用数据结构。在内部它将期望处理文本。

Adding an operator>> for a reference to MyObject won't help when you're actually trying to lexical_cast to a pointer to MyObject. You could conceivably create an operator>>(std::istream&, MyObject*&). However, remember that property_tree is designed for reading configuration from text files, so you'll have the joy of converting your object to and from text.

Don't use property_tree as a generic data structure. Internally it will be expecting to deal with text.

你的笑 2024-12-16 19:21:04

它看起来很像您想要一个序列化解决方案。我在这篇文章中介绍了一些基础知识(包括通过指针存储):

使用指针复制并重新填充结构体实例

此示例还显示了 XML 的序列化,以及包含(可能)重复指针的集合。反序列化时,指针将被忠实地重建(包括重复)。

It is looking a lot like you wanted a serialization solution. I covered some ground (including storing through pointers) in this post:

copying and repopulating a struct instance with pointers

This example also shows serialization to XML, collections containing (potentially) duplicated pointers. On deserialization, the pointers will be reconstructed faithfully (including the duplication).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文