1

Here is my problem. I am supposed to make a python code able to fill a C++ vector of pointer on object of a class of the c++ code. I am using Boost::Python. It's look as follow:

C++ code:

class A_Base {}
class A_derived_1 {}
...
class A_derived_N {}

class B {
...
setAderivediList(vector<A *> &_AderivedList){}
}

What I need to do in Python:

B.setAderivediList(_AderivedList)

I cannot modified the method in the C++ code, I can only add other objects in-between #ifdef python_interface in order not to impact other developpers work. At first, I tried to write a converter from what I found on the Internet, but I failed to make it works for my code. Then I tried to add a _AderivedList in class B and a setter which fill the vector. Though it compile in C++, it dosen't work in Python.

New B class:

class B {
...
setAderivediList(vector<A *> &_AderivedList){}
#ifdef myPython_code
public:
  vector<A *> * _AderivedListPy;
  setAListPy(A * &my_Aderived){       //Actually, I tried without the reference as well
  this->_AderivedListPy->push_back(my_Aderived);
};
#endif
}

In python it becomes:

myB = mydll.B()
Ai = mydll.A_derived_i()
myB.setAListPy(Ai) #stopping here
myB.setAderivediList(myB._AderivedListPy)

If I use the reference, it will throw a "python argument types did not match C++ signature" error message at myB.setAListPy(Ai), without the reference, it will stop at the same line without throwing any error message. Any clue of how I could improve this or any other way to do it? Thank you in advance.

4

1 回答 1

0

实际上,我找到了解决问题的方法。通过在 setAListPy 中直接填充向量(包括调用 setAderivediList)。我只是将一个 int my_A_derived 传递给 setAListPy(实际上,连同所有 A_derived 类构造函数所需的其他 arg)并在 my_A_derived 上使用 if 条件。

于 2013-06-27T17:22:06.010 回答