SWIG:无法访问具有双poin的构造函数

2024-09-27 19:14:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我不太会喝酒。我已经创建了一个python模块来使用c++类。在

我的cpp标题代码是

等级复杂度.h:

class GradedComplex
{
public:
  typedef std::complex<double> dcomplex;
  typedef Item<dcomplex> item_type;
  typedef ItemComparator<dcomplex> comparator;
  typedef std::set<item_type, comparator> grade_type;

private:
  int n_;
  std::vector<grade_type *> grade_;
  std::vector<double> thre_;

public:
  GradedComplex(int n, double *thre);
  ~GradedComplex();

  void push(item_type item);
  void avg(double *buf);
};

CPP代码是

^{pr2}$

我的SWIG接口文件是:

例子。我

/* File: example.i */
%module example
%{
#include "Item.h"
#include "GradedComplex.h"
#include "GradedDouble.h"
%}

%include <std_string.i>
%include <std_complex.i>
%include "Item.h"
%include "GradedComplex.h"
%include "GradedDouble.h"
%template(Int) Item<int>;
%template(Complex) Item<std::complex<double> >;

我通过运行*python生成了python模块设置.pybuild\u ext--inplace*此命令。在

现在我想从python访问GradedComplex(intn,double*thre)

当我试图访问GradedComplex时,它显示 **TypeError:在方法“new\u GradedComplex”中,类型为“double的参数2错误*

如何从python模块传递双指针?请帮我整理一下。在


Tags: 模块代码includetypeitemintgradestd
1条回答
网友
1楼 · 发布于 2024-09-27 19:14:25

直接在构造函数中使用向量并利用SWIG的向量支持更简单:

.i文件中:

%include <std_vector.i>
%template(DoubleVector) std::vector<double>;
%include "GradedComplex.h"

.h中:

^{pr2}$

.cpp中:

GradedComplex::GradedComplex(const vector<double>& dbls) : thre_(dbls)
{
}

n_可以消失,因为{}是同一件事。在

称之为:

c=Item.GradedComplex([1.2,3.4,5.6])

SWIG也可以处理返回向量,因此avg可以是:

std::vector<double> GradedComplex::avg() { ... }

相关问题 更多 >

    热门问题