假设我有两个模块,像这样:
// 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) ...
}
现在在 python 中我想:
import A
import B
a=A.A(...)
B.some_function(a)
但是,它引发了一个 Boost.Python.ArgumentError,表明该调用与 C++ 签名不匹配。
那么如何将 C++ 类的实例传递给在其他模块中使用 boost.python 定义的 C++ 函数?
最佳答案
在模块 B 中公开 some_function 时,使用 boost::python::make_function。 根据 Boost.Python 的文档,
Creates a Python callable object which, when called from Python, converts its arguments to C++ and calls f.
这确保参数被转换为正确的 C++ 类型。所以用
boost::python::def("some_function", boost::python::make_function(&some_function))
相反。
关于python - 如何使用 boost.python 跨模块使用类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41327456/