迭代boost::python时出现TypeError

2024-04-19 22:06:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我得到了一个非常相似的问题: unexpected result iterating over a boost::python vector_indexing_suite

我有以下C++代码:

#include <vector>
#include <boost/python.hpp>
#include <boost/python/suite/indexing/vector_indexing_suite.hpp>

using namespace boost::python;

class Composite {

    public:
        std::string name;
        std::vector<Composite*>& getChildren() {
        return children;
    };

    std::vector<Composite*> children;
};

typedef std::vector<Composite*> CompositeArray;

BOOST_PYTHON_MODULE(coin)
{
    class_<CompositeArray>("CompositeArray")
        .def(vector_indexing_suite<CompositeArray, true>());


    class_<Composite>("Composite", "Composite*", init<>())
        .def("getChildren", &Composite::getChildren, return_internal_reference<>())
        .def_readwrite("name", &Composite::name, "str")
    ;
}

以及以下Python代码:

^{pr2}$

产生错误:

Traceback (most recent call last):
  File "gna.py", line 34, in <module>
    for prout in gna.getChildren():
TypeError: No to_python (by-value) converter found for C++ type: class Composite * __ptr64

这在Boost 1.59下运行良好,但在Boost 1.60下就不行了。在

有什么想法吗?在

编辑: 我试着更新(见下面的建议):

    class_<Composite>("Composite", "Composite*", init<>())

收件人:

    class_<Composite, Composite*>("Composite", "Composite*", init<>())

我确认它是在boost 1.59下工作,而不是boost 1.60。在


Tags: 代码nameincludeinitdefclasssuitestd