2017-03-02 3 views
0

cython으로 포인터를 처리 할 때 문제가 있습니다. 클래스의 cython 구현은 클래스 Person의 C++ 인스턴스에 대한 포인터를 보유합니다.Cython으로 C++ 클래스를 래핑 할 때 포인터 처리

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++ 방법을 person.pyx : 여기 내 .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' 

누군가가 내가 포인터의 ADRESS에서 개체를 얻을 수있는 방법을 알고 있습니까? C++ 프로그램에서 동일한 작업을 수행 할 때 오류가 발생하지 않습니다.

person.cpp에게

Person::Person():parent(NULL){ 
} 

Person::setParent(Person &p){ 
    parent=&p; 
} 

참고 : 여기 당신이 그것을보고 싶은 경우 클래스 ++은 C의 구현입니다 제가 다른 이유로 Person 예 (cdef Peron not_pointer)을 유지하여 해결할 수없는 완전한 클래스를 포함합니다.

답변

1

cython과 함께 C++를 사용하여 전적으로 cython 문서를 읽었어야합니다. 역 참조 연산자를 모르시는 분들은 *을 cython에서 사용할 수 없으므로 cython.operator 모듈에서 dereference을 가져와야합니다. 당신이 지적한 주소에 목표를 접근하고 싶을 경우 dereference(pointer)이라고 써야합니다.

구체적으로 내 문제에 대한 대답은 cdef Person new_father=dereference(father.c_person)입니다.