【发布时间】:2015-11-17 21:06:12
【问题描述】:
我收到错误reference_existing_object_requires_a_pointer_or_reference_return_type。
这是代码。
boost::shared_ptr<CDB::Basic> GetCdbWrapper(boost::shared_ptr<A> cmd)
{
return cmd->Cdb();
}
}
virtual boost::shared_ptr<CDB::Basic> Cdb() {
boost::shared_ptr<CDB::Basic> CdbObj;
return CdbObj;
}
boost::shared_ptr<CDB::Basic> GetCdb() {
return this->Cdb();
}
class_<A, bases<Core::CommandBase>, boost::shared_ptr<A>, boost::noncopyable, >("A",
":description:\n",
boost::python::no_init
)
.def("Cdb", &A::GetCdb,
":description:\n",
return_value_policy<reference_existing_object>()
);
我可以知道上面的代码有什么问题吗?我得到如下编译错误。
error C2027: use of undefined type 'boost::python::detail::reference_existing_object_requires_a_pointer_or_reference_return_type<R>'
1> with
1> [
1> R=result_t
1> ]
1> c:\boost\boost_1_47_0\boost\python\detail\caller.hpp(200) : while compiling class template member function 'PyObject *boost::python::detail::caller_arity<1>::impl<F,Policies,Sig>::operator ()(PyObject *,PyObject *)'
1> with
1> [
1> F=boost::shared_ptr<CDB::Basic> (__thiscall A::* )(void),
1> Policies=boost::python::return_value_policy<boost::python::reference_existing_object>,
1> Sig=boost::mpl::vector2<boost::shared_ptr<CDB::Basic>, A &>
1> ]
【问题讨论】:
-
据我所知(在没有进一步了解 boost 实现的情况下)
reference_existing_object策略要求返回的对象是指针或引用类型。shared_ptr既不是指针也不是引用,而是类类型,它在代码中按值返回。尽管shared_ptr是一个资源管理指针包装类,但它本身并不是一个指针。我认为使用智能指针时不需要reference_existing_object。 -
谢谢。删除 return_value_policy
() 解决了这个问题。
标签: c++ c++11 boost shared-ptr boost-python