Interfacing C++ and Python using SWIG
Question: I try to extend a Matrix class with SWIG to create a python interface. I used
the official Docu code. But I get a totally ridiculous error message.. It
seems to be unimportant where I declare my row class. I always get this error
while compiling. What's wrong with SWIG?

ERROR:

    
    
      ANPyNetCPUPYTHON_wrap.cxx: In function ‘ANN::F2DArray ANN_F2DArray___getitem__(ANN::F2DArray*, int)’:
      ANPyNetCPUPYTHON_wrap.cxx:5192: error: ‘Grid2dRow’ was not declared in this scope
      ANPyNetCPUPYTHON_wrap.cxx:5192: error: expected `;' before ‘r’
      ANPyNetCPUPYTHON_wrap.cxx:5193: error: ‘r’ was not declared in this scope
    

CODE:

    
    
      %{
      #include <AN2DArray.h>
      %}
    
      %include <AN2DArray.h>  
    
      %inline %{
          struct Grid2dRow {
              ANN::F2DArray *g;   // Grid
              int    y;      // Row number
    
              // These functions are used by Python to access sequence types (lists, tuples, ...)
              float __getitem__(int x) {
                  return g->GetValue(x, y);
              }
    
              void __setitem__(int x, float val) {
                  g->SetValue(x, y, val);
              }
          };
      %}
      %extend ANN::F2DArray 
      {
          ANN::F2DArray __getitem__(int y) {
              Grid2dRow r;
              r.g = self;
              r.y = y;
              return r;
          }
      };
    


Answer: I found the problem after I looked into the generated *.cxx. The SWIG docu IS
wrong in two points. Below is the code which is working. Maybe someone
wondering about the same issue is helped with it.

    
    
      %{
      #include <AN2DArray.h>
      %}
    
      %inline %{
          struct Grid2dRow {
              ANN::F2DArray *g;   // Grid
              int    y;      // Row number
    
              // These functions are used by Python to access sequence types (lists, tuples, ...)
              float __getitem__(int x) {
                  return g->GetValue(x, y);
              }
    
              void __setitem__(int x, float val) {
                  g->SetValue(x, y, val);
              }
          };
      %}
    
      %include <AN2DArray.h>  
    
      %addmethods ANN::F2DArray {
          Grid2dRow __getitem__(int y) {
              Grid2dRow r;
              r.g = self;
              r.y = y;
              return r;
          }
      };
    

