SWIG:无法使用双指针访问构造函数 [英] SWIG : Unable to access constructor with double pointer
问题描述
我是SWIG的新人。我创建了一个使用c ++类的python模块。
我的cpp头文件是
GradedComplex.h:
class GradedComplex
{
public:
typedef std :: complex< double&复合物
typedef Item< dcomplex>物品种类;
typedef ItemComparator< dcomplex>比较器
typedef std :: set< item_type,comparator> grade_type;
private:
int n_;
std :: vector< grade_type *>年级_;
std :: vector< double> thre_;
public:
GradedComplex(int n,double * thre);
〜GradedComplex();
void push(item_type item);
void avg(double * buf);
}
且CPP代码为
#include< iostream>
#includeGradedComplex.h
using namespace std;
GradedComplex :: GradedComplex(int n,double * thre)
{
n_ = n;
for(int i = 0; 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 {
if(item.norm()< thre_ [i])
{
grade_ [i] - &
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;
}
}
我的SWIG界面文件是:
example.i
/ * File:example.i * /
%模块示例
%{
#includeItem.h
#includeGradedComplex.h
#includeGradedDouble.h
%}
%include< std_string.i>
%include< std_complex.i>
%includeItem.h
%includeGradedComplex.h
%includeGradedDouble.h
%template(Int)Item< int>
%template(Complex)Item< std :: complex< double> > ;;
我已经通过运行 * python setup.py build_ext --inplace * 此命令。
现在我想从python访问 GradedComplex(int n,double * thre) b
$ b
当我尝试访问 GradedComplex 时,它显示
** TypeError:在方法'new_GradedComplex' >'错误*
如何从python模块传递双指针?请帮助我排序这个问题。
使用向量直接在构造函数中并利用SWIG的向量支持:
在 .i
文件中:
%include< std_vector.i>
%template(DoubleVector)std :: vector< double> ;;
%包括GradedComplex.h
在 .h
:
GradedComplex(const std :: vector< double>& dbls)
在 .cpp
:
GradedComplex :: GradedComplex(const vector< double& dbls):thre_(dbls)
{
}
n _
c $ c> thre_.size()是一回事。
调用它:
c = Item.GradedComplex([1.2,3.4,5.6])
SWIG可以处理返回向量,因此 avg
可以是:
std :: vector< double> GradedComplex :: avg(){...}
I am new to SWIG. I have created a python module to use c++ classes.
My cpp header code is
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);
};
And CPP code is
#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;
}
}
My SWIG interface file is :
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> >;
I have generated python module by running *python setup.py build_ext --inplace* this command.
And now I want to access GradedComplex(int n, double *thre) from python
When I tried to access GradedComplex it shows **TypeError: in method 'new_GradedComplex', argument 2 of type 'double ' error*
How do I pass double pointer from python module? Please help me to sort this issue.
It is simpler to use a vector directly in the constructor and take advantage of SWIG's vector support:
In the .i
file:
%include <std_vector.i>
%template(DoubleVector) std::vector<double>;
%include "GradedComplex.h"
In the .h
:
GradedComplex(const std::vector<double>& dbls);
In the .cpp
:
GradedComplex::GradedComplex(const vector<double>& dbls) : thre_(dbls)
{
}
n_
can go away, since thre_.size()
is the same thing.
Call it with:
c=Item.GradedComplex([1.2,3.4,5.6])
SWIG can handle returning vectors as well, so avg
can be:
std::vector<double> GradedComplex::avg() { ... }
这篇关于SWIG:无法使用双指针访问构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!