1

我尝试使用 SWIG 扩展一个 Matrix 类来创建一个 python 接口。我使用了官方的Docu代码。但是我收到一条完全荒谬的错误消息。我在哪里声明我的行类似乎并不重要。编译时总是出现这个错误。SWIG 有什么问题?

错误:

  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

代码:

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

1 回答 1

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;
      }
  };
于 2013-06-06T08:22:53.253 回答