Cython C++ wrapper operator() overloading error

匿名 (未验证) 提交于 2019-12-03 02:33:02

问题:

Connected with my previous question. Using Cython to wrap a C++ class that uses OpenCV types as parameters

Now I'm stuck in another error. My cython wrapping code of the OpenCV type Matx33d looks like:

cdef extern from "opencv2/core/core.hpp" namespace "cv":     cdef cppclass Matx33d "cv::Matx<double, 3, 3>":         Matx33d()         Matx33d(double v0, double v1, double v2, double v3, double v4, double v5, double v6, double v7, double v8)         double& operator()(int i, int j) 

Then I define a function to copy the Matx33d to a numpy array.

cdef Matx33d2numpy(Matx33d &m):     cdef np.ndarray[np.double_t, ndim=2] np_m = np.empty((3,3), dtype=np.float64)       np_m[0,0]= m(0,0); np_m[0,1]= m(0,1); np_m[0,2]= m(0,2)     np_m[1,0]= m(1,0); np_m[1,1]= m(1,1); np_m[1,2]= m(1,2)     np_m[2,0]= m(2,0); np_m[2,1]= m(2,1); np_m[2,2]= m(2,2)         return np_m 

When I compile the cython wrapper I get these error

geom_gateway.cpp(2528) error C3861: '()': identifier not found 

This corresponds to the first use of Matx33d::operator(), that's when accessing m(0,0) in the code above. If I look at the generated geom_gateway.cpp line 2528 I get:

  *__Pyx_BufPtrStrided2d(__pyx_t_5numpy_double_t *, __pyx_pybuffernd_np_m.rcbuffer->pybuffer.buf, __pyx_t_6, __pyx_pybuffernd_np_m.diminfo[0].strides, __pyx_t_7, __pyx_pybuffernd_np_m.diminfo[1].strides) = operator()(0, 0); 

I don't understand this operator()(0, 0) there alone at the end of the line without any object!! How is this possible? Is this a Cython bug? or is the syntax I'm using for the operator() wrong? Any help is appreciated!

回答1:

Ok, I don't know why that error happened, to me it looks like the syntax

double& operator()(int i, int j) 

should work, but it doesn't. This syntax does work for other operators like +,-,/,*

An alternative syntax that does work is the following:

double& get "operator()"(int i, int j) 

then in cython code when we want to use the operator()(i,j) instead we call get(i, j)



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!