使用SWIG连接C ++和Python

时间:2013-06-05 09:54:55

标签: c++ python swig

我尝试使用SWIG扩展Matrix类以创建python接口。我使用了官方的Docu代码。但我得到一个完全荒谬的错误信息.. 在我声明我的行类似乎并不重要。编译时我总是遇到这个错误。 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;
      }
  };

1 个答案:

答案 0 :(得分:0)

在查看生成的* .cxx后,我发现了问题。 SWIG文件在两点上是错误的。以下是正在运行的代码。也许有人想知道同样的问题是有帮助的。

  %{
  #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;
      }
  };