考虑以下示例:
#include "Python.h"
#include <boost/python.hpp>
#include <boost/shared_ptr.hpp>
class A {};
class B : public A{};
void foo(boost::shared_ptr<A>& aptr) { }
BOOST_PYTHON_MODULE(mypy)
{
using namespace boost::python;
class_<A, boost::shared_ptr<A> >("A", init<>());
class_<B, boost::shared_ptr<B>, bases<A> >("B", init<>());
def("foo", foo);
}
如果我调用python代码
import mypy
b = mypy.B()
mypy.foo(b)
我得到了
ArgumentError: Python argument types in
mypy.foo(B)
did not match C++ signature:
foo(boost::shared_ptr<A> {lvalue})
我搜索了很多,但我找不到一个好的解释/修复/解决方法。非常欢迎任何帮助!
答案 0 :(得分:4)
问题在于您要求对shared_ptr<A>
进行非const引用,而Python中的b
实例不包含一个;它包含shared_ptr<B>
。虽然shared_ptr<B>
可以隐式转换为shared_ptr<A>
,但shared_ptr<B>&
无法隐式转换为shared_ptr<A>&
。
如果您可以修改foo
以获取shared_ptr<A>
或shared_ptr<A> const &
,那么这将解决您的问题。
如果没有,您还需要打包一个接受shared_ptr<B>&
。