簡體   English   中英

Cython-嘗試訪問指向struct的指針的內容時出錯

[英]Cython - error when trying to access contents of pointer to struct

我在Cython中有一個cdef ed類,它看起來與此非常相似:

cdef class AprilTagDetector:
    cdef capriltag.apriltag_detector_t* _apriltag_detector

    def __cinit__(self):
        self._apriltag_detector = capriltag.apriltag_detector_create();
        # standard null checks
    # standard __dealloc__(self) here

    property quad_decimate:
        def __get__(self):
            return self._apriltag_detector.quad_decimate

相應的.pxd文件如下所示:

cdef extern from "apriltag.h":
    # The detector itself
    ctypedef struct apriltag_detector_t:
        pass

    # Detector constructor and destructor
    apriltag_detector_t* apriltag_detector_create()
    void apriltag_detector_destroy(apriltag_detector_t* td);

問題是,當我去編譯此代碼時,它會吐出此錯誤:

property quad_decimate:
    def __get__(self):
        return self._apriltag_detector.quad_decimate             ^
------------------------------------------------------------

apriltags.pyx:47:14: Cannot convert 'apriltag_detector_t *' to Python object

這里發生了什么? 我還無法從Cython文檔中找出答案。

值得慶幸的是,當我與一個黑客空間的朋友一起進行這個項目時,我發現了問題所在。 問題出在ctypedef struct apriltag_detector_t塊中。 當我在塊中編寫pass時,我認為Cython會自動計算出該結構的內部內容,並讓我訪問所需的元素-這里為quad_decimate

不是這樣 為了使Cython理解結構的內容,您必須這樣告訴它結構中的內容:

ctypedef struct apriltag_detector_t:
    float quad_decimate

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM