cythonでポインタを扱うときに問題があります。クラスのcython実装は、クラスPerson
のC++インスタンスへのポインタを保持します。CythonでC++クラスをラップするときのポインタの処理
person.pyx
cdef class PyPerson:
cdef Person *pointer
def __cinit__(self):
self.pointer=new Person()
def set_parent(self, PyPerson father):
cdef Person new_father=*(father.pointer)
self.c_person.setParent(new_father)
引数としてPerson
オブジェクトを受け取りsetParent
C++方法:ここに私の.pyx
ファイルです。 PyPerson
クラスの属性pointer
はPerson
オブジェクトへのポインタであるため、*pointer
というアドレスのオブジェクトを*(PyPersonObject.pointer)
という形で取得できたと考えました。私はそれをコンパイルしようとすると、しかし、私は
def set_parent(self, PyPerson father):
cdef Person new_father=*(father.pointer)
^
------------------------------------------------------------
person.pyx:51:30: Cannot assign type 'Person *' to 'Person'
次のエラーを取得する誰かが、私はポインタのアドレスで、オブジェクトに得ることができる方法を知っていますか? 私がC++プログラムで同じことをしても、エラーは発生しません。ここではC++クラスの実装は、あなたがそれを見たい場合には:
person.cpp
Person::Person():parent(NULL){
}
Person::setParent(Person &p){
parent=&p;
}
注:私は他の理由Person
インスタンス(cdef Peron not_pointer
)を保持することによってそれを解決することはできません完全なクラスを含む。