Cython C ++ wrapper operator()重载错误 [英] Cython C++ wrapper operator() overloading error
问题描述
与我上一个问题连接。
使用Cython包装使用OpenCV类型作为参数的C ++类
现在我又陷入了另一个错误。我的cython包装代码的OpenCV类型Matx33d看起来像:
cdef extern从opencv2 / core / core.hpp命名空间 cv:
cdef cppclass Matx33dcv :: 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&然后我定义一个函数来将Matx33d复制到一个函数中,然后将这个函数复制到一个函数中。 numpy数组。 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
编译cython包装我得到这些错误
geom_gateway.cpp(2528)错误C3861:'()':identifier not found
这对应于Matx33d :: operator()的第一次使用,当访问m )在上面的代码中。
如果我看看生成的geom_gateway.cpp行2528我得到:
* __ 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)
我不明白这个operator()(0,0)线没有任何对象!这怎么可能?这是一个Cython bug吗?或者是我为operator()使用的语法错了?
任何帮助是感谢!
解决方案好吧,我不知道为什么这个错误发生,像语法
double& operator()(int i,int j)
此语法适用于其他运算符,如+, - ,/,*
可以使用的替代语法如下:
double& getStyle()(int,int j)
然后在cython代码中使用operator()(i,j),而不是调用get(i,j)
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!
解决方案 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)
这篇关于Cython C ++ wrapper operator()重载错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!