Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ boost::python函数调用中boost::shared_ptr的转换_C++_Python_Boost_Shared Ptr_Boost Python - Fatal编程技术网

C++ boost::python函数调用中boost::shared_ptr的转换

C++ boost::python函数调用中boost::shared_ptr的转换,c++,python,boost,shared-ptr,boost-python,C++,Python,Boost,Shared Ptr,Boost Python,考虑以下示例: #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;

考虑以下示例:

#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);
}
我明白了

ArgumentError: Python argument types in
    mypy.foo(B)
did not match C++ signature:
    foo(boost::shared_ptr<A> {lvalue})
ArgumentError:中的Python参数类型
米皮·福(B)
与C++签名不匹配:
foo(boost::shared_ptr{lvalue})

我在谷歌上搜索了很多,但是我找不到一个很好的解释/修复/解决方法。欢迎任何帮助

问题在于,您请求的是对
共享\u ptr
的非常量引用,而Python中的
b
实例根本不包含一个;它包含一个
共享的\u ptr
。虽然
shared_ptr
可以隐式转换为
shared_ptr
shared_ptr&
不能隐式转换为
shared_ptr&

如果您可以修改
foo
以获取
shared\u ptr
,或
shared\u ptr const&
,这将解决您的问题


如果没有,您还需要包装一个接受
shared\u ptr&

的版本,如果使用
foo
获取
boost::python::object
参数,然后从中提取
shared\u ptr
?当然可以,但我希望有一种方法不为foo编写任何额外的包装器(我需要的东西很多)
ArgumentError: Python argument types in
    mypy.foo(B)
did not match C++ signature:
    foo(boost::shared_ptr<A> {lvalue})