[英]How to use class across modules with boost.python?
Say I have two modules, like this: 说我有两个模块,像这样:
// A.cpp
class A{ /*... */};
BOOST_PYTHON_MODULE(A){
boost::python::class_<A>("A")...
}
// B.cpp
#include "a.hpp"
int some_function(A a) { /* do something */ }
BOOST_PYTHON_MODULE(B){
boost::python::def("some_function", some_function) ...
}
And now in python I'd like to: 现在在python中,我想:
import A
import B
a=A.A(...)
B.some_function(a)
However, it raised a Boost.Python.ArgumentError indicating that the call didn't match C++ signature. 但是,它引发了一个Boost.Python.ArgumentError,指示该调用与C ++签名不匹配。
So how can I pass an instance of a C++ class to a C++ function defined in other module with boost.python? 那么如何通过boost.python将C ++类的实例传递给其他模块中定义的C ++函数呢?
When exposing some_function in module B, use boost::python::make_function. 在模块B中公开some_function时,请使用boost :: python :: make_function。 According to the Boost.Python's documentation, 根据Boost.Python的文档,
Creates a Python callable object which, when called from Python, converts its arguments to C++ and calls f. 创建一个Python可调用对象,当从Python调用该对象时,将其参数转换为C ++并调用f。
this ensures that the arguments are converted to the right C++ types. 这样可以确保将参数转换为正确的C ++类型。 So use 所以用
boost::python::def("some_function", boost::python::make_function(&some_function))
instead. 代替。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.