我是一名经验丰富的 Python 程序员,正在尝试学习 C++ 以加快某些项目的速度。将 py::tuple 传递给函数,如何访问元素?
这是我正在制作的用于保存视频中的图像的类的构造函数。
Buffer::Buffer(int size, py::tuple hw_args_) {
unsigned int frame_height = get<0>(hw_args_);
unsigned int frame_width = get<1>(hw_args_);
Buffer::time_codes = new int[size];
Buffer::time_stamps = new string[size];
Buffer::frames = new unsigned char[size][hw_args[0]][hw_args[1]];
if ((Buffer::time_codes && Buffer::time_stamps) == 0) {
cout << "Error allocating memory\n";
exit(1);
}
}
这给了我一个错误“没有重载函数‘get’的实例与参数列表匹配,参数类型是 pybind11::tuple”
我也尝试过以这种方式设置框架高度和宽度。
unsigned int frame_height = hw_args_[0];
unsigned int frame_width = hw_args_[1];
这会给出错误“不存在从‘pybind11::detail::tuple_accessor’到‘unsigned int’的合适转换函数”
我很茫然,我似乎只能找到有关在 C++ 中制作元组的信息,而无法从 Pybind11 / Python 访问它们。
py::tuple
有一个 operator[]
,它返回一个 detail::tuple_accessor
,可以转换为 py::object
,所以以下应该可以工作。
uint32_t frame_height = hw_args_[0].cast<uint32_t>();