unexpected result iterating over a boost::python vector_indexing_suite

杀马特。学长 韩版系。学妹 提交于 2019-12-11 00:18:10

问题


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

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

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
        ;
}

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

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

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

I got this error message:

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?

Thank you.


回答1:


Try

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

Instead of

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


来源:https://stackoverflow.com/questions/11292869/unexpected-result-iterating-over-a-boostpython-vector-indexing-suite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!