Cython: Invalid operand types for '+' (btV

2019-07-15 10:34发布

问题:

bullet.pxd:

cdef extern from "bullet/LinearMath/btVector3.h":
    cdef cppclass btVector3:
        btVector3(float, float, float) except +
        btVector3 operator+(const btVector3&, const btVector3&)

btmath.pyx:

cimport bullet as bt

cdef class Vector:

    cdef bt.btVector3* _this

    def __cinit__(self, float x=0, float y=0, float z=0):

        self._this = new bt.btVector3(x, y, z)


    def __add__(Vector self, Vector other):

        vec = Vector()
        del vec._this
        vec._this[0] = self._this[0] + other._this[0]

        return vec

The prototype of operator+ in btVector3.h:

SIMD_FORCE_INLINE btVector3 
operator+(const btVector3& v1, const btVector3& v2);

As stated in the title, I get "Invalid operand types for '+' (btVector3; btVector3)". I'm guessing it has something to with how Cython deals with pass by reference, maybe?

Any help would be greatly appreciated.

回答1:

As it turns out, Cython treats the "this" parameter as implicit in this case, so bullet.pxd should look like this:

cdef extern from "bullet/LinearMath/btVector3.h":
    cdef cppclass btVector3:
        btVector3(float, float, float) except +
        btVector3 operator+(btVector3)

Big thanks to Robert Bradshaw, for helping me figure this out: https://groups.google.com/forum/#!topic/cython-users/8LtEE_Nvf0o



标签: cython