意外的结果迭代一个boost :: python vector_indexing_suite [英] unexpected result iterating over a boost::python vector_indexing_suite

查看:336
本文介绍了意外的结果迭代一个boost :: python vector_indexing_suite的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已成功包装一个名为Composite的类。
此类具有以下方法:

I have wrapped successfully a class named Composite. This class has the following method:

std::vector<Composite*> Composite::getChildren();

我试图使用vector_indexing_suite包装返回的std :: vector,如下:
[snippet]

I tried to wrap the returned std::vector using the vector_indexing_suite, in this way: [snippet]

typedef std::vector<Composite*> CompositeArray;

BOOST_PYTHON_MODULE(composite)
{   
    class_<CompositeArray>("CompositeArray")
        .def(vector_indexing_suite<CompositeArray, true>());


    class_<Composite>("Composite", init<>())
        ... more wrapper
        .def("getChildren", &Composite::getChildren)
        ... more wrapper
        ;
}

现在一切似乎都正常工作,当我从python调用getChildren它正确返回一个包装的CompositeArray。我可以做,例如:

Now everything seem working correctly and when I call the getChildren() method from python it returns correctly a wrapped CompositeArray. I can do, for example:

from composite import Composite
myComp = Composite()

myComp.addChild('childA')
myComp.addChild('childB')

len(myComp.getChildren())  #returns 2
myComp.getChildren()[0] # returns the first child of type Composite

但是当我尝试遍历CompositeArray时,像这样:

But when I try to iterate over the CompositeArray, like in this way:

for child in myComp.getChildren():
    # do something with child...

我收到此错误讯息:

TypeError: No to_python (by-value) converter found for C++ type: class Composite * __ptr64

这对我没有任何意义,考虑到索引访问工作完美!
我坚持这个...你对我做错了什么有任何想法吗?

which doesn't make any sense to me, considering that access by index worked perfectly! I'm stuck on this... do you have any idea of what I'm doing wrong?

谢谢。

推荐答案

尝试

class_<Composite, Composite*>("Composite", init<>())

而不是

class_<Composite>("Composite", init<>())

这篇关于意外的结果迭代一个boost :: python vector_indexing_suite的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆