问题
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