我是SWIG的新手。我創建了一個python模塊來使用C++類。SWIG:無法使用雙指針訪問構造函數
我CPP頭的代碼是
GradedComplex.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代碼
#include <iostream>
#include "GradedComplex.h"
using namespace std;
GradedComplex::GradedComplex(int n, double *thre)
{
n_ = n;
for (int i = 0; i < n_; ++i)
{
thre_.push_back(thre[i]);
grade_.push_back(new grade_type());
}
}
GradedComplex::~GradedComplex()
{
while (0 < grade_.size())
{
delete grade_.back();
grade_.pop_back();
}
}
void GradedComplex::push(item_type item)
{
for (int i = 0; i < n_; ++i)
{
if (item.norm() < thre_[i])
{
grade_[i]->insert(item);
break;
}
}
}
void GradedComplex::avg(double *buf)
{
for (int i = 0; i < n_; ++i)
{
int n = 0;
double acc = .0l;
for (grade_type::iterator it = grade_[i]->begin(); it != grade_[i]->end(); ++it)
{
acc += (*it).norm();
++n;
}
buf[i] = acc/n;
}
}
我痛飲接口文件是:
example.i
/* 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 setup.py build_ext --inplace *這個命令生成了python模塊。
,現在我想訪問GradedComplex蟒蛇
(INT N,雙* THRE)當我試圖訪問GradedComplex它顯示 **類型錯誤:在方法 'new_GradedComplex',說法2的類型'雙重'錯誤*
如何從python模塊傳遞雙指針?請幫我解決這個問題。
*如何*你嘗試調用'GradedComplex'?你通過了什麼論點? – molbdnilo
我已經通過這些LEVEL = 3,thre = [1.0,10.0,100.0] GradedComplex(LEVEL,thre) –