【问题标题】:Boost.Python boost::shared_ptr no to python by value converter foundBoost.Python boost::shared_ptr no to python by value 转换器找到
【发布时间】:2016-01-19 10:21:00
【问题描述】:

我有给定的代码,

在 A.hpp 中

typedef boost::shared_ptr<A> APtr;   

在 B.hpp 中

typedef std::vector<APtr> APtrCollection;                      
typedef boost::shared_ptr<APtrCollection> APtrCollectionPtr;
APtrCollectionPtr a_ptr;

APtrCollectionPtr B::get_items() {
     return a_ptr;
}

在 boost.python 接口文件中

class_<B>("St")
        .def("get", get_st)
        .def("set", set_st)
        .add_property("items", &B::get_items)
;

代码编译没有错误,但是当我运行时,我得到了错误

     TypeError: 'No to_python (by-value) converter found for C++ type: boost::shared_ptr<std::vector<boost::shared_ptr<A>, std::allocator<boost::shared_ptr<A> > > >'

如何解决这个问题?

我的增强版本是:1.57, 蟒蛇:Python 2.7.11 |Anaconda 2.4.1(64 位), gcc:gcc 版本 4.8.3 (GCC)

【问题讨论】:

    标签: python c++ boost shared-ptr boost-python


    【解决方案1】:

    您需要先将AAPtrCollection 暴露给boost::python

    class_<A>("A")
        // You can put stuff here or just leave it blank
        ;
    
    class_<APtrCollection>("APtrCollection")
        .def(vector_indexing_suite<APtrCollection>())
        ;
    

    现在像以前一样继续:

    class_<B>("St")
        .def("get", get_st)
        .def("set", set_st)
        .add_property("items", &B::get_items)
        ;
    

    【讨论】:

      猜你喜欢
      • 2011-09-13
      • 2011-07-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-03
      • 2011-04-22
      • 1970-01-01
      相关资源
      最近更新 更多