【发布时间】:2013-06-04 15:35:35
【问题描述】:
我有一个 C++ 标头,它返回一个包含 3 个元素的结构。如何让 python 正确接受 struct 变量?
这就是我在 C++ 函数中所拥有的:
// Function name myfunc
struct velocity
{
std::vector< std::vector<double> > u;
std::vector< std::vector<double> > v;
std::vector< std::vector<double> > w;
};
velocity velo; //Creating object
velo.u = sum(umean,pu);
velo.v = sum(vmean,pv);
velo.w = sum(wmean,pw);
return(velo)
这是我使用 SWIG 后的 Python 实现
import numpy
from myfunc import * # importing C++ myfunc library
My = 100 # Matrix dimensions
Mz = 100
z = myfunc(My,Mz) # Supplying the matrix dimensions to the myfunc library
print(z)
执行此操作时收到的错误消息:
<myfunc.velocity; proxy of <Swig Object of type 'velocity *' at 0x2951ae0> >
我知道我必须以某种方式在 SWIG 中定义以使 python 采用“原样”的结构。 有什么办法吗?或者您可能建议的任何替代方法?这是我的 SWIG 文件
%module myfunc
%{
#include "myfunc.h"
%}
%include "std_vector.i"
// Instantiate templates used by example
namespace std {
%template(IntVector) vector<int>;
%template(DoubleVector) vector<double>;
%template(twodvector) std::vector< std::vector<double> >;
}
struct velocity
{
std::vector< std::vector<double> > u;
std::vector< std::vector<double> > v;
std::vector< std::vector<double> > w;
};
%include "myfunc.h"
请注意,我在这里声明了一个结构。这在 SWIG 上编译成功,但我不知道如何在 Python 中使用它来实际获取 C++ 结构!
【问题讨论】:
-
请同时发布您的 swig 接口文件
标签: c++ python-2.7 c++11 struct swig