SWIG:传递std :: vector&lt; std :: vector <double>&gt;指向python的指针

时间:2018-05-04 16:00:18

标签: python c++ swig

我正在使用SWIG从python调用带有三个参数的C ++程序。最后一个参数也用于返回值。

analysis.i

#ifndef _COVERAGE_
#define _COVERAGE_
#include<vector>
void coverage(std::vector<double> *Pypar,
              std::vector<std::vector<double>> *Pyxpass,
              std::vector<std::vector<double>> *Pyypass);
#endif

coverage.hpp

#include "isofuneval.hpp"
#include "funeval_base.hpp"
#include <boost/numeric/ublas/vector.hpp>
#include <boost/numeric/ublas/matrix.hpp> 

void coverage (std::vector<double> *Pypar,
               std::vector<std::vector<double>> *Pyxpass,
               std::vector<std::vector<double>> *Pyypass){

  //Convert the par, xpass and ypass vectors as ublas
  boost::numeric::ublas::vector<double> par((*Pypar).size());
  boost::numeric::ublas::matrix<double> xpass((*Pyxpass).size(),(*Pyxpass)[0].size());
  boost::numeric::ublas::matrix<double> ypass((*Pyypass).size(),(*Pyypass)[0].size());

  for (size_t i = 0; i < (*Pypar).size(); i++){
    par(i) = (*Pypar)[i];
  }

  for (size_t i = 0; i < (*Pyxpass).size(); i++){
    for (size_t j = 0; j <(*Pyxpass)[0].size(); j++){
      xpass(i,j) = (*Pyxpass)[i][j] ;}
  }

  for (size_t i = 0; i < (*Pyypass).size(); i++){
    for (size_t j = 0; j <(*Pyypass)[0].size(); j++){
      ypass(i,j) = (*Pyypass)[i][j];}
  }

  isofuneval CoveragePlot;

  CoveragePlot.function(par, xpass, ypass); //These should actually be references and or pointers if I expect ypass to have the result

  for (size_t i = 0; i < (*Pyypass).size(); i++){
    for (size_t j = 0; j <(*Pyypass)[0].size(); j++){
      (*Pyypass)[i][j] = ypass(i,j) ;
    }
  }
}

coverage.cpp

import _coverage as     coverage
coverage.coverage([3, 2, 1 ],[[4, 8423] , [4, 12] ],[[24,234 ], [23, 23] ])

它编译,模块加载,但是当我运行它时:

{{1}}

我收到以下错误:

TypeError:在方法'coverage'中,参数1的类型为'std :: vector&lt;双,性病::分配器&LT;双&gt; &GT; *'

1 个答案:

答案 0 :(得分:0)

analysis.i文件中明确声明模板专精

%template(DoubleVector1D) std::vector<double>;
%template(DoubleVector2D) std::vector<std::vector<double>>;