python - 使用 Boost Python 公开带有带有数组参数的构造函数的类

标签 python c++ class boost constructor

我正在尝试使用 Boost Python 导出一个类,如下所示:

struct bool_array
{
    bool_array(bool constructor_bool[7])
    {
        for(unsigned int i=0; i < 7; i++)
            bools[i] = constructor_bool[i];
    }

    bool bools[7];
};

我还想使用以下 Boost 代码公开构造函数:

class_<bool_array>("bool_array", init<bool*>())
    .def_readwrite("bools", &bool_array::bools)
;

问题是我收到此编译器错误:

error C2440: '=' : cannot convert from 'const bool [7]' to 'bool [7]'

我也尝试过

init<bool[7]>

init<bool[]>

没有效果。

我确信我错过了一些明显的东西,但我一直无法弄清楚我需要做什么来公开这个类。

谢谢

最佳答案

在思考这个问题时,我了解到 boost-python 不支持直接暴露 C 风格数组。相反,我选择使用 vector :

struct bool_array
{
    bool_array(std::vector<bool> constructor_bool)
    {
        for(unsigned int i=0; i < 7; i++)
            bools.push_back(constructor_bool[i]);
    }

     std::vector<bool> bools;
};

使用以下 boost-python 包装器:

typedef std::vector<bool> BoolVector;
bp::class_<BoolVector>("BoolVector")
    .def(bp::vector_indexing_suite<BoolVector>())
;

bp::class_<bool_array>("bool_array", bp::init<std::vector<bool>>())
    .def_readwrite("bools", &bool_array::bools)
;

关于python - 使用 Boost Python 公开带有带有数组参数的构造函数的类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24414769/

相关文章:

python - 如何删除嵌套列表中的负值

python - 在多列上使用 numpy 二维数组从 Pandas 数据框中选择行

c++ - C++ 中用户定义数组的问题

java - 从类方法显示类对象

arrays - 在Lua中将数组作为函数参数传递?

css - div 和链接/表单的问题

python - 如何从 tkinter 中按下的按钮获取网格信息?

python - 导入错误:Cloud9 中没有名为 MapReduce 的模块

c++ - 如何在 TaskScheduler 中显示任务触发器?

c++ - 为什么我的STM32F7每43秒才中断一次?