gpt4 book ai didi

c++ - Boost::python - 在 python 类的对象上填充指针的 C++ vector

转载 作者:太空宇宙 更新时间:2023-11-04 14:05:11 28 4
gpt4 key购买 nike

这是我的问题。我应该使 python 代码能够在 C++ 代码的类的对象上填充指针的 C++ vector 。我正在使用 Boost::Python。它看起来如下:

C++代码:

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

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

我需要用 Python 做什么:

B.setAderivediList(_AderivedList)

我无法修改 C++ 代码中的方法,我只能在 #ifdef python_interface 之间添加其他对象,以免影响其他开发人员的工作。起初,我尝试根据我在 Internet 上找到的内容编写一个转换器,但未能使其适用于我的代码。然后我尝试在 B 类中添加一个 _AderivedList 和一个填充 vector 的 setter。虽然它在 C++ 中编译,但在 Python 中不起作用。

新B类:

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
}

在 python 中它变成了:

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

如果我使用引用,它会在 myB.setAListPy(Ai) 处抛出“python 参数类型与 C++ 签名不匹配”错误消息,如果没有引用,它会在同一行停止而不抛出任何错误消息。关于我如何改进这个或任何其他方法的任何线索?提前谢谢你。

最佳答案

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

关于c++ - Boost::python - 在 python 类的对象上填充指针的 C++ vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17340303/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com