![](/img/trans.png)
[英]How/When to use a Cython Extension Type vs a Cython Struct to store data that is passed around class functions
[英]How to use pointer to typedef struct in Cython Extension Type
說我的頭文件example.h
具有以下typedef struct
:
typedef struct A A;
我正在嘗試在Cy.thon擴展類型中使用指向A
的指針作為test.pyx
的類變量,然后在對A
的引用上調用初始化函數f
:
cdef class Test:
cdef A* a
def __cinit__(self):
self.a = a
f(&a)
...
編譯`test.pyx時,出現以下編譯錯誤:
Error compiling Cython file:
------------------------------------------------------------
...
cdef class Test:
cdef A* a
def __cinit__(self):
self.a = a
^
------------------------------------------------------------
test.pyx: undeclared name not builtin: a
Error compiling Cython file:
------------------------------------------------------------
...
cdef class Test:
cdef A* a
def __cinit__(self):
self.a = a
^
------------------------------------------------------------
test.pyx: Cannot convert Python object to 'A *'
顯然,它沒有識別出對象a
,而是將其解釋為python對象。 我怎樣才能解決這個問題?
這是因為使用前必須先聲明它。 例如,您在c代碼example.h
具有此代碼:
typedef struct struct_name{
int a;
float b;
}struct_alias;
然后,您的.pyx
文件應如下所示:
cdef extern from "example.h":
ctypedef struct struct_alias:
int a
int b
cdef class Test:
cdef A* a
def __cinit__(self):
self.a = a
f(&a)
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.