title: Cython - Exposing C++ (vector and non-vector) objects returned by C++ function to Python
body: <p>I am working on a project where I have a large C++ code base available which I want to wrap with cython and made available in python.</p>
  
  <p>In doing so, I am facing a situation where some of my C++ functions returns Vector object or simple object (with multiple attributes). I want to return this object to Python so that its values can be accessed.</p>
  
  <p>For doing that,  I am following this post almost exactly: <a href="https://stackoverflow.com/questions/33677231/how-to-expose-a-function-returning-a-c-object-to-python-without-copying-the-ob">How to expose a function returning a C++ object to Python without copying the object?</a></p>
  
  <p>I have a very similar requirement. Please refer to the <strong>move</strong> construct that is implemented/used in the above forum. </p>
  
  <p>Following is my code that I am trying to implement for simple (non-vector case):</p>
  
  <p><strong>test_header.pxd</strong></p>
  
  <pre class="lang-py prettyprint-override"><code>from libcpp.vector cimport vector
  from libcpp.string cimport string
  from libcpp.map cimport map
  
  cdef extern from "main_class.h":
  
      cdef cppclass main_class:
          int ID1
          double ID2
  
  
  cdef extern from "class2.h":
  
      cdef cppclass class2:
          class2() except +
          class2(const double&amp; T) except +
          void Add(const main_class&amp; ev)
          const vector[main_class]&amp; GetEvs() 
  
  #Process Class
  cdef extern from "Pclass.h":
  
      cdef cppclass Pclass:
          Pclass(const unsigned&amp; n, const unsigned&amp; dims) except +
          unsigned GetDims()        
          double processNext(const class2&amp; data, const unsigned&amp; num_iter) 
  
  
  cdef extern from "main_algo.h":
  
      #TODO: Check if inheritance works correctly, virtual functions, objects, std::vector
      cdef cppclass main_algo:
          main_algo(const unsigned&amp; dims) except +
          main_class getNext(Pclass&amp; pr, const class2&amp; d)
  </code></pre>
  
  <p><strong>test.pyx</strong></p>
  
  <pre class="lang-py prettyprint-override"><code>from header cimport main_class, class2, Pclass, main_algo
  from libcpp.vector cimport vector
  from libcpp.string cimport string
  from libcpp.map cimport map
  
  
  cdef extern from "&lt;utility&gt;":
      vector[class2]&amp;&amp; move(vector[class2]&amp;&amp;)
      main_class&amp;&amp; move(main_class&amp;&amp;)
  
  cdef class main_class_2:
      cdef main_class* thisptr
      cdef main_class_3 evs
  
      def __cinit__(self,main_class_3 evs):
          self.evs = evs
          self.thisptr = &amp;evs.evs
  
  cdef class main_class_3:
  
      cdef main_class evs
  
      cdef move_from(self, main_class&amp;&amp; move_this):
          self.evs = move(move_this)
  
  
  cdef class implAlgo:
  
      cdef:
          main_algo *_thisptr
  
      def __cinit__(implAlgo self):
          self._thisptr = NULL
  
      def __init__(implAlgo self, unsigned dims):
          self._thisptr = new main_algo(dims)
  
      def __dealloc__(implAlgo self):
          if self._thisptr != NULL:
              del self._thisptr
  
  
      cdef int _check_alive(implAlgo self) except -1:
          if self._thisptr == NULL:
              raise RuntimeError("Wrapped C++ object is deleted")
          else:
              return 0      
  
      cdef getNext(implAlgo self, Pclass&amp; p, const class2&amp; s):
          self._check_alive()
          cdef main_class evs = self._thisptr.getNext(p, sq)
          retval = main_class_3()
          retval.move_from(move(evs))
  
          return retval
  </code></pre>
  
  <p>Here, the class <code>main_algo</code> implements the method <code>getNext()</code> which returns an object of class <code>main_class</code>.</p>
  
  <p>From test.pyx, I want to return this object to purely python file where its values can be accessed. </p>
  
  <p>When I try to compile the above code, I get multiple instances of 
  following error at several places wherever I use that method and I get similar error for different tokens like ')' or '*' . Some example errors are: </p>
  
  <pre><code>sources.cpp:5121:70: error: expected primary-expression before ‘*’ token
     __pyx_vtable_11Cython_test_7sources_main_class_3.move_from = (PyObject *(*)(struct __pyx_obj_11Cython_test_7sources_main_class_3 *, main_class &amp;&amp;))__pyx_f_11Cython_test_7sources_8main_class_3_move_from;
                                                                        ^
  sources.cpp:5121:73: error: expected primary-expression before ‘)’ token
     __pyx_vtable_11Cython_test_7sources_main_class_3.move_from = (PyObject *(*)(struct __pyx_obj_11Cython_test_7sources_main_class_3 *, main_class &amp;&amp;))__pyx_f_11Cython_test_7sources_8main_class_3_move_from;
                                                                           ^
  sources.cpp:5121:75: error: expected primary-expression before ‘struct’
     __pyx_vtable_11Cython_test_7sources_main_class_3.move_from = (PyObject *(*)(struct __pyx_obj_11Cython_test_7sources_main_class_3 *, main_class &amp;&amp;))__pyx_f_11Cython_test_7sources_8main_class_3_move_from;
                                                                             ^
  sources.cpp:5121:133: error: expected primary-expression before ‘&amp;&amp;’ token
     __pyx_vtable_11Cython_test_7sources_main_class_3.move_from = (PyObject *(*)(struct __pyx_obj_11Cython_test_7sources_main_class_3 *, main_class &amp;&amp;))__pyx_f_11Cython_test_7sources_8main_class_3_move_from;
  </code></pre>
  
  <p>But all these tokens are related to the objects that I create to move the C++ object to python. There is no error with any other declaration. </p>
  
  <p>Can someone please help to show me where am I wrong?</p>