【发布时间】:2017-11-02 15:50:08
【问题描述】:
我正在编写一个 c++ 库,我希望能够在 python 中调用它。 我很想使用 swig 我能够成功地创建和编译 python 模块,但是我在理解如何连接 python 和 c++ 方面有点挣扎。
struct twod {
double x; ///< x value
double y; ///< y value
};
double distance_calculation(std::vector <twod> A, std::vector <twod> B);
这是我的头文件的快照。按照我的 .i 文件:
%module hausdorff
%{
#include "Hausdorff.h"
using namespace hausdorff;
%}
%include "std_vector.i"
%include "Hausdorff.h"
namespace std {
%template(vector2d) vector<twod>;
}
在 python 中我可以创建对象:
In [13]: vector = hausdorff.vector2d
In [14]: vector = ([1,2], [3,4])
In [15]: result = hausdorff.distance_calculation(vector, vector)
我得到错误:
TypeError: in method 'distance_calculation', argument 1 of type 'std::vector< hausdorff::twod,std::allocator< hausdorff::twod > >'
如何将正确的对象传递给函数?
【问题讨论】: