简体   繁体   中英

Boost.Python boost::shared_ptr no to python by value converter found

I have the given code,

In A.hpp

typedef boost::shared_ptr<A> APtr;   

and

In B.hpp

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

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

In boost.python interface file

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

The code compiles without errors, but when i run, i get error

     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> > > >'

How to solve this problem ?

My boost version is: 1.57, python: Python 2.7.11 |Anaconda 2.4.1 (64-bit), gcc: gcc version 4.8.3 (GCC)

You need to expose A and APtrCollection to boost::python first:

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

class_<APtrCollection>("APtrCollection")
    .def(vector_indexing_suite<APtrCollection>())
    ;

Now just proceed as before:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM