Swig: interface simple C++ class that returns vector
Swig: interface simple C++ class that returns vector
我对swig完全陌生,如果看起来简单请见谅。
我想为 C++
中的 class 编写一个接口,该接口获取 return 双精度向量:
/* 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'
感谢您的指导或评论。
example.i
中的最后一行应该是 %include "example.h"
而不是 #include "example.h"
。
我对swig完全陌生,如果看起来简单请见谅。
我想为 C++
中的 class 编写一个接口,该接口获取 return 双精度向量:
/* 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'
感谢您的指导或评论。
example.i
中的最后一行应该是 %include "example.h"
而不是 #include "example.h"
。