以下のようなfloatのポインタを引数として受け取るクラス内関数があるとき、どのようにpython側から呼べば良いのでしょうか?
#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); }
以下のようにpython側で呼んでいるのですが、エラーが出ていて困っている状況です。
import numpy as np 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, arr.size) # Print the updated array print(arr)
以下がエラーです。
Traceback (most recent call last):
File "test2.py", line 13, in <module>
my_obj.my_func(arr, arr.size)
TypeError: my_func(): incompatible function arguments. The following argument types are supported:
1. (self: example.MyClass, arg0: float, arg1: int) -> None
Invoked with: <example.MyClass object at 0x7f987db50ef0>, array([1., 2., 3.], dtype=float32), 3

回答1件
あなたの回答
tips
プレビュー
下記のような回答は推奨されていません。
このような回答には修正を依頼しましょう。
また依頼した内容が修正された場合は、修正依頼を取り消すようにしましょう。