我正在尝试使用 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/