简体   繁体   English

使用SWIG连接C ++和Python

[英]Interfacing C++ and Python using SWIG

I try to extend a Matrix class with SWIG to create a python interface. 我尝试使用SWIG扩展Matrix类以创建python接口。 I used the official Docu code. 我使用了官方的Docu码。 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? 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;
      }
  };

I found the problem after I looked into the generated *.cxx. 在查看生成的* .cxx之后,我发现了问题。 The SWIG docu IS wrong in two points. SWIG文档有两点是错误的。 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;
      }
  };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM