Cython c++ example fails, why?
Question: When I tried to run the example on
<http://docs.cython.org/src/userguide/wrapping_CPlusPlus.html>, I got the
following error on the mac terminal:

    
    
    Error compiling Cython file:
    
     distutils: language = c++
     distutils: sources = Rectangle.cpp
    
    cdef extern from "Rectangle.h" namespace "shapes":
    cdef cppclass Rectangle:
    
    
    rect.pyx:5:0: Expected an increase in indentation level
    Traceback (most recent call last):
      File "setup.py", line 8, in <module>
        language="c++",                        # generate and compile C++ code
      File "/usr/local/lib/python2.7/site-packages/Cython/Build/Dependencies.py", line 877, in cythonize
        cythonize_one(*args)
      File "/usr/local/lib/python2.7/site-packages/Cython/Build/Dependencies.py", line 997, in cythonize_one
        raise CompileError(None, pyx_file)
    Cython.Compiler.Errors.CompileError: rect.pyx
    

* * *

I am not sure what is wrong with my code. Could you please tell me why I am
getting the error message?

I have Cython 0.23.4 and Python 2.7.10 in my machine. I do not have any
problem running the python/c++ codes. I also installed boost and boost-python
on my computer.

Briefly, I created the following files:

1-Rectangle.h

2-Rectangle.cpp

3-setup.py

4-rect.pyx

Then, I did "python setup.py build_ext --inplace"

**Rectangle.h**

    
    
    #include <stdio.h>
    namespace shapes {
        class Rectangle {
        public:
            int x0, y0, x1, y1;
            Rectangle(int x0, int y0, int x1, int y1);
            ~Rectangle();
            int getLength() const;
            int getHeight() const;
            int getArea() const;
            void move(int dx, int dy);
        };
    }
    

**Rectangle.cpp**

    
    
    #include "Rectangle.h"
    
    using namespace shapes;
    
    Rectangle::Rectangle(int X0, int Y0, int X1, int Y1) {
        x0 = X0;
        y0 = Y0;
        x1 = X1;
        y1 = Y1;
    }
    
    Rectangle::~Rectangle() {}
    
    int Rectangle::getLength() const {
        return (x1 - x0);
    }
    
    int Rectangle::getHeight() const {
        return (y1 - y0);
    }
    
    int Rectangle::getArea() const {
        return getLength() * getHeight();
    }
    
    void Rectangle::move(int dx, int dy) {
        x0 += dx;
        y0 += dy;
        x1 += dx;
        y1 += dy;
    }
    

**setup.py**

    
    
    from distutils.core import setup, Extension
    from Cython.Build import cythonize
    
    setup(ext_modules = cythonize(Extension(
                                            "rect",                                # the extesion name
                                            sources=["rect.pyx", "Rectangle.cpp"], # the Cython source and
                                            # additional C++ source files
                                            language="c++",                        # generate and compile C++ code
                                            )))
    

I tried the following, too:

    
    
    from distutils.core import setup
    from Cython.Build import cythonize
    
    setup(ext_modules = cythonize(
               "rect.pyx",                 # our Cython source
               sources=["Rectangle.cpp"],  # additional source file(s)
               language="c++",             # generate C++ code
          ))
    

**rect.pyx**

    
    
    # distutils: language = c++
    # distutils: sources = Rectangle.cpp
    
    cdef extern from "Rectangle.h" namespace "shapes":
    cdef cppclass Rectangle:
    Rectangle(int, int, int, int) except +
    int x0, y0, x1, y1
    int getLength() const
    int getHeight() const
    int getArea() const
    void move(int, int)
    
    cdef class PyRectangle:
    cdef Rectangle *thisptr
    def __cinit__(self, int x0, int y0, int x1, int y1):
    self.thisptr = new Rectangle(x0, y0, x1, y1)
    def __dealloc__(self):
    del self.thisptr
    def getLength(self):
    return self.thisptr.getLength()
    def getHeight(self):
    return self.thisptr.getHeight()
    def getArea(self):
    return self.thisptr.getArea()
    def move(self, dx, dy):
    self.thisptr.move(dx, dy)
    


Answer: Thanks so much, swenzel! Now, it works!

**rect.pyx**

    
    
    # distutils: language = c++
    # distutils: sources = Rectangle.cpp
    cdef extern from "Rectangle.h" namespace "shapes":
        cdef cppclass Rectangle:
            Rectangle(int, int, int, int) except +
            int x0, y0, x1, y1
            int getLength()
            int getHeight()  
            int getArea()  
            void move(int, int)
    
    cdef class PyRectangle:
        cdef Rectangle *thisptr
        def __cinit__(self, int x0, int y0, int x1, int y1):
            self.thisptr = new Rectangle(x0, y0, x1, y1)
        def __dealloc__(self):
            del self.thisptr
        def getLength(self):
            return self.thisptr.getLength()
        def getHeight(self):
            return self.thisptr.getHeight()
        def getArea(self):
            return self.thisptr.getArea()
        def move(self, dx, dy):
            self.thisptr.move(dx, dy)
    

