python - 遍历 boost::python vector 时出现 TypeError

标签 python c++ boost stdvector

我遇到了一个非常相似的问题: 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 代码:

import coin

gna = coin.Composite()
gna.name = "test"
gna2 = coin.Composite()
gna2.name = "test2"

gna.getChildren().append(gna2)

for slip in gna.getChildren():
      print(slip.name)

产生错误:

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。

最佳答案

改变

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

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

帮助 boost 1.55。

关于python - 遍历 boost::python vector 时出现 TypeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35056398/

相关文章:

c++ - Boost库中用于动态位集的硬件支持的popcount

c++ - 按顺序查找boost::multi_index,按顺序获取下一个元素

c++ - 使用vector和pointer的双下标重载的区别

python - 使用 lxml 获取第一个元素的属性

python-requests 认证代理 httplib.BadStatusLine

python - Pygame 大表面

c++ - 加速我的球体体积代码(嵌套 while 循环)

c++ - 如何为 C++ 程序定义 MSYS Makefile 的路径?

android - Android 上 Boost 的链接错误

python - 如何在 Ubuntu 16.04 中创建 Python 3.8 虚拟环境