title: How to build in Cython an iostream object (e. g. cout) and pass it to a c++ function?
body: <p>I'm trying to wrap the following C++ class:<br/>
  print.h<br/></p>
  
  <pre><code>#include &lt;string&gt;
  using namespace std;
  class ControlParameters
  {
  public:
      ControlParameters();
      ~ControlParameters();
      void Initialize(string input, ostream&amp; log);
  };
  </code></pre>
  
  <p>print.cpp<br/></p>
  
  <pre><code>#include "print.h"
  #include &lt;iostream&gt;
  
  ControlParameters::ControlParameters()
  {
  }
  ControlParameters::~ControlParameters()
  {
  }
  void ControlParameters::Initialize(string input, ostream&amp; log)
  {
   log&lt;&lt;"Output = "&lt;&lt; input &lt;&lt;endl;
  }
  </code></pre>
  
  <p>My String.pyx looks as follows:<br/></p>
  
  <pre><code>from libcpp.string cimport string
  cdef extern from "&lt;iostream&gt;" namespace "std":
      cdef cppclass ostream:
      ┆   ostream&amp; write(const char*, int) except +
  
  cdef extern from "print.h":
      cdef cppclass ControlParameters:
      ┆   ControlParameters() except +
      ┆   void Initialize(string, ostream&amp;)
  
  cdef class PyControlParameters:
      cdef ControlParameters *thisptr
      def __cinit__(self):
      ┆   self.thisptr = new ControlParameters()
      def __dealloc__(self):
      ┆   del self.thisptr
      def PyInitialize(self, a,b):
              self.thisptr.Initialize(a, b)
  </code></pre>
  
  <p>After I compiled the String.pyx I get the following Error:</p>
  
  <pre><code>Compiling String.pyx because it changed.
  [1/1] Cythonizing String.pyx
  
  Error compiling Cython file:
  ------------------------------------------------------------
  ...
      def __cinit__(self):
          self.thisptr = new ControlParameters()
      def __dealloc__(self):
          del self.thisptr
      def PyInitialize(self, a,b):
              self.thisptr.Initialize(a, b)
                                        ^
  ------------------------------------------------------------
  
  String.pyx:18:39: Cannot convert Python object to 'ostream'
  Traceback (most recent call last):
    File "setup.py", line 7, in &lt;module&gt;
      language="c++"
    File "/usr/local/lib/python2.7/dist-packages/Cython/Build/Dependencies.py", line 1039, in cythonize
      cythonize_one(*args)
    File "/usr/local/lib/python2.7/dist-packages/Cython/Build/Dependencies.py", line 1161, in cythonize_one
      raise CompileError(None, pyx_file)
  Cython.Compiler.Errors.CompileError: String.pyx
  </code></pre>
  
  <p>I read <a href="https://stackoverflow.com/questions/30984078/cython-working-with-c-streams">here</a> that I have to build my own ostream class in order to get an ostream object. But how do I define this object? 
  I think i need a function which converts my python object to an object of ostream. How could I implement such a function?</p>