Swig - 结构的 c++ 向量以及如何连接它们
Swig - c++ vector of a struct and how to interface them
我正在编写一个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 > >'
如何将正确的对象传递给函数?
它比这复杂一些,至少没有更多的工作:
>>> import hausdorff
>>> v = hausdorff.vector2d()
>>> a = hausdorff.twod()
>>> a.x = 1.2
>>> a.y = 2.3
>>> v.push_back(a)
>>> a.x = 3.4
>>> a.y = 4.5
>>> v.push_back(a)
>>> test.distance_calculation(v,v)
如果为结构提供构造函数,则可以简化:
>>> test.distance_calculation([test.twod(1.2,3.4),test.twod(1.2,3.4)],
[test.twod(4.5,6.7),test.twod(1.2,3.4)])
如果您提供类型图转换,我将其作为练习(或另一个 SO 问题:^)这可以做到:
>>> test.distance_calculation([(1.1,2.2),(3.3,4.4)],[(5.5,6.6),(7.7,8.8)])
相关文章:
- 当套接字连接断开时检测C/C++Unix
- 无法在windows上使用mingw将sqlite3与c连接
- 到连接组件算法的问题(递归)
- QTcpSocket在不阻塞GUI的情况下重新连接到服务器
- 无法在C++中建立与MySQL数据库的连接
- PC中的程序和PHONE中的本机描述应用程序之间的数据连接
- 在Qt Creator中,如何在连接到正在运行的进程后查看控制台输出
- 连接 dockerized 模型和 dockerized 数据库时出现"无法 SQLConnect"错误
- 使用 bfs 解决连接组件问题时得到错误的答案
- 在C++的两个字符串中连接以逗号分隔的唯一值
- 如何修复valgrind启动时的致命错误(与libc6-dbg和libc6-dbg:i386连接)
- C++应用程序 MySQL odbc 数据库连接错误:在引发"otl_tmpl_exception<>"实例后终止调用
- OSX蓝牙打开RFCOMMChannelAsync声称已连接,但未建立连接,并且从未调用过委托
- 在 libcurl 连接池中预创建连接
- 套接字连接"Operation not permitted"错误,甚至使用升压/平发器根.cpp
- libcurl 和 DNS ttl 中的内部连接管理
- 如何应用 libcurl 的持久连接选项
- 提升 ASIO - io_service 不要等待连接到线程
- 使用 c++ 为大至 1000 x 1000 的矩阵实现 8 连接连接的组件标记
- 头可连接连接是否可以具有争用条件?你如何绕过它