Boost property_tree 用于存储指针
是否可以在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
添加运算符>>当您实际尝试 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.
它看起来很像您想要一个序列化解决方案。我在这篇文章中介绍了一些基础知识(包括通过指针存储):
使用指针复制并重新填充结构体实例
此示例还显示了 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).