【发布时间】:2017-09-23 07:41:08
【问题描述】:
我在使用 swig 方面是全新的,如果看起来很简单,请原谅我。
我想为C++ 中的一个类编写一个接口,该接口获取并返回一个双精度向量:
/* example.h file */
#include <iostream>
#include <cstdio>
#include <vector>
class my_class
{
private:
int N;
public:
my_class(){ }
std::vector<double> half(const std::vector<double>& );
};
/* example.cpp file */
#include "example.h"
std::vector<double> my_class::half(const std::vector<double>& v) {
std::vector<double> w(v);
for (unsigned int i=0; i<w.size(); i++)
w[i] /= 2.0;
return w;
}
和example.i接口文件
%module example
%{
#define SWIG_FILE_WITH_INIT
#include "example.h"
%}
%include "std_vector.i"
namespace std {
%template(DoubleVector) vector<double>;
}
#include "example.h"
然后我尝试在python中使用该模块:
import example
A = example.my_class()
AttributeError Traceback (most recent call last)
<ipython-input-5-d2af384d4adf> in <module>()
----> 1 example.my_class()
AttributeError: 'module' object has no attribute 'my_class'
感谢任何指导或评论。
【问题讨论】: