我正在尝试将带有一些 2D 数组成员变量的 C++ 类传递给 Python 脚本。二维数组直接分配到类中,独立于构造函数。我正在使用 boost_python 库进行此转换,而包装函数使用 add_property/reference_existing_object 函数,期望它从 C++ 脚本的双指针创建一个新的 Python 2D 数组。
但是,我收到一条错误消息,告诉我在处理数组时 boost 无法从 const double 转换为 double 。但是数组不应该首先设置为 const 吗?有可能我在这里。我是新手,自从我写 C++ 以来已经有一段时间了,所以我希望你们中的一些人能以一种简单的方式帮助解释这一点:) 如果我的方法不是要走的路,我怎么能传递这些数组?
我在这里发布类和包装函数以供参考。
class Interface
{
public:
double zUpPatch1[NVALUES_UPSWEEP][N_VALUES_IN_TUPLE+1];
double zUpPatch2[NVALUES_UPSWEEP][N_VALUES_IN_TUPLE+1];
double zDownPatch1[NVALUES_UPSWEEP][N_VALUES_IN_TUPLE+1];
double zDownPatch2[NVALUES_UPSWEEP][N_VALUES_IN_TUPLE+1];
bool bNewData[N_VALUES_IN_TUPLE+1];
int mrows, ncols;
Interface();
void GetRawData(int);
};
Interface::Interface()
{
mrows = NVALUES_UPSWEEP;
ncols = N_VALUES_IN_TUPLE+1;
}
BOOST_PYTHON_MODULE(GetRawDataPy_ext)
{
using namespace boost::python;
class_<Interface>("Interface", init<>())
.def("GetRawData", &Interface::GetRawData)
.def_readwrite("mrows", &Interface::mrows)
.def_readwrite("ncols", &Interface::ncols)
.def_readwrite("bNewData", &Interface::bNewData)
.add_property("zUpPatch1",
make_getter(&Interface::zUpPatch1, return_value_policy<reference_existing_object>()),
make_setter(&Interface::zUpPatch1, return_value_policy<reference_existing_object>()))
.add_property("zUpPatch2",
make_getter(&Interface::zUpPatch2, return_value_policy<reference_existing_object>()),
make_setter(&Interface::zUpPatch2, return_value_policy<reference_existing_object>()))
.add_property("zDownPatch1",
make_getter(&Interface::zDownPatch2, return_value_policy<reference_existing_object>()),
make_setter(&Interface::zDownPatch2, return_value_policy<reference_existing_object>()))
.add_property("zDownPatch2",
make_getter(&Interface::zDownPatch2, return_value_policy<reference_existing_object>()),
make_setter(&Interface::zDownPatch2, return_value_policy<reference_existing_object>()))
.add_property("bNewData",
make_getter(&Interface::bNewData, return_value_policy<reference_existing_object>()),
make_setter(&Interface::bNewData, return_value_policy<reference_existing_object>()))
;
}
这是来自 boost 编译器的错误消息的摘录:
C:\...\.../data_members.hpp(64) : error C2440: '=' : cannot convert from
'const double [1000][5]' to double [1000][5]'
There is no context in wich this conversion is possible
C:\...\.../data_members.hpp(63) : while compiling class template member function
'void boost::python::detail::member<Data,Class>::operator ()(Class &,const double (&)) const'
with
[
Data=double [1000][5],
Class=Class
]
提前致谢!