I want to call a c++ class function (with a float pointer as argument) in python using pybind11.
For example, the following c++ code.
#include <pybind11/numpy.h>
#include <pybind11/pybind11.h>
namespace py = pybind11;
class MyClass {
public:
void myFunc(float *arr, int size) {
for (int i = 0; i < size; i++) {
arr[i] += 1.0f;
}
}
};
PYBIND11_MODULE(example, m) {
py::class_<MyClass>(m, "MyClass")
.def(py::init<>())
.def("my_func", &MyClass::myFunc);
}
The following python code called the class.
import numpy as np
import ctypes
import pybind11
import sys
sys.path.append( "./build" )
import example
# Create an instance of MyClass
my_obj = example.MyClass()
# Create a NumPy array
arr = np.array([1.0, 2.0, 3.0], dtype=np.float32)
# Call myFunc with the NumPy array
my_obj.my_func(arr.ctypes.data_as(pybind11.c_float_p), arr.size)
# Print the updated array
print(arr)
However, the following error occurred
Traceback (most recent call last):
File "test2.py", line 16, in
my_obj.my_func(arr.ctypes.data_as(pybind11.c_float_p), arr.size)
AttributeError: module 'pybind11' has no attribute 'c_float_p'
How should I deal with this?
Best regards.