使用SWIG和Python处理vector<vector<double>>参数

4
我正在尝试使用SWIG将返回2D向量的C++函数包装为Python。我的文件名为functions.h。
#include <vector>

std::vector< std::vector<double> >  array_mean(std::vector< std::vector<double> > array)
{

  std::vector< std::vector<double> > mean_array( rows, std::vector<double>(cols));
....

  return mean_array;

}

在接口文件 functions.i 中,我有以下内容:
%module functions
%{
#include "functions.h"
%}
%include "std_vector.i"
namespace std {
  %template(VecVecdouble) vector< vector<double> >;
}

%include "functions.h"

然后我执行

swig -c++ -python functions.i

g++ -O2 -fPIC -c functions_wrap.cxx -I/usr/include/python2.4 -I/usr/lib/python2.4

但编译器报了很多错误。

functions_wrap.cxx: In function 'bool swig::check(PyObject*) [with Type = double]':

functions_wrap.cxx:3763:   instantiated from 'bool    swig::PySequence_Cont<T>::check(bool) const [with T = double]'
functions_wrap.cxx:3820:   instantiated from 'static int swig::traits_asptr_stdseq<Seq, T>::asptr(PyObject*, Seq**) [with Seq = std::vector<double, std::allocator<double> >, T = double]'

有什么想法是问题出在哪里吗?谢谢。
编辑以包括完整的编译器输出。
functions_wrap.cxx:3878:   instantiated from 'static int 

swig::traits_asptr<std::vector<T, std::allocator<_CharT> > >::asptr(PyObject*, std::vector<T, std::allocator<_CharT> >**) [with T = double]'
functions_wrap.cxx:3030:   instantiated from 'int swig::asptr(PyObject*, Type**) [with Type = std::vector<double, std::allocator<double> >]'
functions_wrap.cxx:3152:   instantiated from 'static bool swig::traits_check<Type, swig::pointer_category>::check(PyObject*) [with Type = std::vector<double, std::allocator<double> >]'
functions_wrap.cxx:3159:   instantiated from 'bool swig::check(PyObject*) [with Type = std::vector<double, std::allocator<double> >]'
functions_wrap.cxx:3763:   instantiated from 'bool swig::PySequence_Cont<T>::check(bool) const [with T = std::vector<double, std::allocator<double> >]'
functions_wrap.cxx:3820:   instantiated from 'static int swig::traits_asptr_stdseq<Seq, T>::asptr(PyObject*, Seq**) [with Seq = std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >, T = std::vector<double, std::allocator<double> >]'
functions_wrap.cxx:3878:   instantiated from 'static int swig::traits_asptr<std::vector<T, std::allocator<_CharT> > >::asptr(PyObject*, std::vector<T, std::allocator<_CharT> >**) [with T = std::vector<double, std::allocator<double> >]'
functions_wrap.cxx:3030:   instantiated from 'int swig::asptr(PyObject*, Type**) [with Type = std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >]'
functions_wrap.cxx:4946:   instantiated from here
functions_wrap.cxx:3159: error: no type named 'category' in 'struct swig::traits<double>'
functions_wrap.cxx: In function 'const char* swig::type_name() [with Type = double]':
functions_wrap.cxx:2946:   instantiated from 'static swig_type_info* swig::traits_info<Type>::type_info() [with Type = double]'
functions_wrap.cxx:2953:   instantiated from 'swig_type_info* swig::type_info() [with Type = double]'
functions_wrap.cxx:2987:   instantiated from 'static PyObject* swig::traits_from_ptr<Type>::from(Type*, int) [with Type = double]'
functions_wrap.cxx:2993:   instantiated from 'static PyObject* swig::traits_from<Type>::from(const Type&) [with Type = double]'
functions_wrap.cxx:3005:   instantiated from 'PyObject* swig::from(const Type&) [with Type = double]'
functions_wrap.cxx:3862:   instantiated from 'static PyObject* swig::traits_from_stdseq<Seq, T>::from(const Seq&) [with Seq = std::vector<double, std::allocator<double> >, T = double]'
functions_wrap.cxx:3885:   instantiated from 'static PyObject* swig::traits_from<std::vector<T, std::allocator<_CharT> > >::from(const std::vector<T, std::allocator<_CharT> >&) [with T = double]'
functions_wrap.cxx:3005:   instantiated from 'PyObject* swig::from(const Type&) [with Type = std::vector<double, std::allocator<double> >]'
functions_wrap.cxx:3862:   instantiated from 'static PyObject* swig::traits_from_stdseq<Seq, T>::from(const Seq&) [with Seq = std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >, T = std::vector<double, std::allocator<double> >]'
functions_wrap.cxx:3885:   instantiated from 'static PyObject* swig::traits_from<std::vector<T, std::allocator<_CharT> > >::from(const std::vector<T, std::allocator<_CharT> >&) [with T = std::vector<double, std::allocator<double> >]'
functions_wrap.cxx:3005:   instantiated from 'PyObject* swig::from(const Type&) [with Type = std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >]'
functions_wrap.cxx:6430:   instantiated from here
functions_wrap.cxx:2936: error: 'type_name' is not a member of 'swig::traits<double>'
functions_wrap.cxx: In function 'Type swig::as(PyObject*, bool) [with Type = double]':
functions_wrap.cxx:3563:   instantiated from 'swig::PySequence_Ref<T>::operator T() const [with T = double]'
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algobase.h:285:   instantiated from 'static _OI std::__copy<_BoolType, std::random_access_iterator_tag>::copy(_II, _II, _OI) [with _II = swig::PySequence_InputIterator<double, const swig::PySequence_Ref<double> >, _OI = double*, bool _BoolType = false]'
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algobase.h:317:   instantiated from '_OI std::__copy_aux(_II, _II, _OI) [with _II = swig::PySequence_InputIterator<double, const swig::PySequence_Ref<double> >, _OI = double*]'
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algobase.h:326:   instantiated from 'static _OI std::__copy_normal<<anonymous>, <anonymous> >::copy_n(_II, _II, _OI) [with _II = swig::PySequence_InputIterator<double, const swig::PySequence_Ref<double> >, _OI = double*, bool <anonymous> = false, bool <anonymous> = false]'
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algobase.h:387:   instantiated from '_OutputIterator std::copy(_InputIterator, _InputIterator, _OutputIterator) [with _InputIterator = swig::PySequence_InputIterator<double, const swig::PySequence_Ref<double> >, _OutputIterator = double*]'
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/vector.tcc:230:   instantiated from 'void std::vector<_Tp, _Alloc>::_M_assign_aux(_ForwardIterator, _ForwardIterator, std::forward_iterator_tag) [with _ForwardIterator = swig::PySequence_InputIterator<double, const swig::PySequence_Ref<double> >, _Tp = double, _Alloc = std::allocator<double>]'
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_vector.h:853:   instantiated from 'void std::vector<_Tp, _Alloc>::_M_assign_dispatch(_InputIterator, _InputIterator, __false_type) [with _InputIterator = swig::PySequence_InputIterator<double, const swig::PySequence_Ref<double> >, _Tp = double, _Alloc = std::allocator<double>]'
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_vector.h:319:   instantiated from 'void std::vector<_Tp, _Alloc>::assign(_InputIterator, _InputIterator) [with _InputIterator = swig::PySequence_InputIterator<double, const swig::PySequence_Ref<double> >, _Tp = double, _Alloc = std::allocator<double>]'
functions_wrap.cxx:3801:   instantiated from 'void swig::assign(const PySeq&, Seq*) [with PySeq = swig::PySequence_Cont<double>, Seq = std::vector<double, std::allocator<double> >]'
functions_wrap.cxx:3816:   instantiated from 'static int swig::traits_asptr_stdseq<Seq, T>::asptr(PyObject*, Seq**) [with Seq = std::vector<double, std::allocator<double> >, T = double]'
functions_wrap.cxx:3878:   instantiated from 'static int swig::traits_asptr<std::vector<T, std::allocator<_CharT> > >::asptr(PyObject*, std::vector<T, std::allocator<_CharT> >**) [with T = double]'
functions_wrap.cxx:3030:   instantiated from 'int swig::asptr(PyObject*, Type**) [with Type = std::vector<double, std::allocator<double> >]'
functions_wrap.cxx:3152:   instantiated from 'static bool swig::traits_check<Type, swig::pointer_category>::check(PyObject*) [with Type = std::vector<double, std::allocator<double> >]'
functions_wrap.cxx:3159:   instantiated from 'bool swig::check(PyObject*) [with Type = std::vector<double, std::allocator<double> >]'
functions_wrap.cxx:3763:   instantiated from 'bool swig::PySequence_Cont<T>::check(bool) const [with T = std::vector<double, std::allocator<double> >]'
functions_wrap.cxx:3820:   instantiated from 'static int swig::traits_asptr_stdseq<Seq, T>::asptr(PyObject*, Seq**) [with Seq = std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >, T = std::vector<double, std::allocator<double> >]'
functions_wrap.cxx:3878:   instantiated from 'static int swig::traits_asptr<std::vector<T, std::allocator<_CharT> > >::asptr(PyObject*, std::vector<T, std::allocator<_CharT> >**) [with T = std::vector<double, std::allocator<double> >]'
functions_wrap.cxx:3030:   instantiated from 'int swig::asptr(PyObject*, Type**) [with Type = std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >]'
functions_wrap.cxx:4946:   instantiated from here
functions_wrap.cxx:3138: error: no type named 'category' in 'struct swig::traits<double>'
[jdensmor@ls10977 test]$ g++ -O2 -fPIC -c functions_wrap.cxx -I/usr/include/python2.4 -I/usr/lib/python2.4
functions_wrap.cxx: In function 'bool swig::check(PyObject*) [with Type = double]':
functions_wrap.cxx:3763:   instantiated from 'bool swig::PySequence_Cont<T>::check(bool) const [with T = double]'
functions_wrap.cxx:3820:   instantiated from 'static int swig::traits_asptr_stdseq<Seq, T>::asptr(PyObject*, Seq**) [with Seq = std::vector<double, std::allocator<double> >, T = double]'
functions_wrap.cxx:3878:   instantiated from 'static int swig::traits_asptr<std::vector<T, std::allocator<_CharT> > >::asptr(PyObject*, std::vector<T, std::allocator<_CharT> >**) [with T = double]'
functions_wrap.cxx:3030:   instantiated from 'int swig::asptr(PyObject*, Type**) [with Type = std::vector<double, std::allocator<double> >]'
functions_wrap.cxx:3152:   instantiated from 'static bool swig::traits_check<Type, swig::pointer_category>::check(PyObject*) [with Type = std::vector<double, std::allocator<double> >]'
functions_wrap.cxx:3159:   instantiated from 'bool swig::check(PyObject*) [with Type = std::vector<double, std::allocator<double> >]'
functions_wrap.cxx:3763:   instantiated from 'bool swig::PySequence_Cont<T>::check(bool) const [with T = std::vector<double, std::allocator<double> >]'
functions_wrap.cxx:3820:   instantiated from 'static int swig::traits_asptr_stdseq<Seq, T>::asptr(PyObject*, Seq**) [with Seq = std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >, T = std::vector<double, std::allocator<double> >]'
functions_wrap.cxx:3878:   instantiated from 'static int swig::traits_asptr<std::vector<T, std::allocator<_CharT> > >::asptr(PyObject*, std::vector<T, std::allocator<_CharT> >**) [with T = std::vector<double, std::allocator<double> >]'
functions_wrap.cxx:3030:   instantiated from 'int swig::asptr(PyObject*, Type**) [with Type = std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >]'
functions_wrap.cxx:4946:   instantiated from here
functions_wrap.cxx:3159: error: no type named 'category' in 'struct swig::traits<double>'
functions_wrap.cxx: In function 'const char* swig::type_name() [with Type = double]':
functions_wrap.cxx:2946:   instantiated from 'static swig_type_info* swig::traits_info<Type>::type_info() [with Type = double]'
functions_wrap.cxx:2953:   instantiated from 'swig_type_info* swig::type_info() [with Type = double]'
functions_wrap.cxx:2987:   instantiated from 'static PyObject* swig::traits_from_ptr<Type>::from(Type*, int) [with Type = double]'
functions_wrap.cxx:2993:   instantiated from 'static PyObject* swig::traits_from<Type>::from(const Type&) [with Type = double]'
functions_wrap.cxx:3005:   instantiated from 'PyObject* swig::from(const Type&) [with Type = double]'
functions_wrap.cxx:3862:   instantiated from 'static PyObject* swig::traits_from_stdseq<Seq, T>::from(const Seq&) [with Seq = std::vector<double, std::allocator<double> >, T = double]'
functions_wrap.cxx:3885:   instantiated from 'static PyObject* swig::traits_from<std::vector<T, std::allocator<_CharT> > >::from(const std::vector<T, std::allocator<_CharT> >&) [with T = double]'
functions_wrap.cxx:3005:   instantiated from 'PyObject* swig::from(const Type&) [with Type = std::vector<double, std::allocator<double> >]'
functions_wrap.cxx:3862:   instantiated from 'static PyObject* swig::traits_from_stdseq<Seq, T>::from(const Seq&) [with Seq = std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >, T = std::vector<double, std::allocator<double> >]'
functions_wrap.cxx:3885:   instantiated from 'static PyObject* swig::traits_from<std::vector<T, std::allocator<_CharT> > >::from(const std::vector<T, std::allocator<_CharT> >&) [with T = std::vector<double, std::allocator<double> >]'
functions_wrap.cxx:3005:   instantiated from 'PyObject* swig::from(const Type&) [with Type = std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >]'
functions_wrap.cxx:6430:   instantiated from here
functions_wrap.cxx:2936: error: 'type_name' is not a member of 'swig::traits<double>'
functions_wrap.cxx: In function 'Type swig::as(PyObject*, bool) [with Type = double]':
functions_wrap.cxx:3563:   instantiated from 'swig::PySequence_Ref<T>::operator T() const [with T = double]'
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algobase.h:285:   instantiated from 'static _OI std::__copy<_BoolType, std::random_access_iterator_tag>::copy(_II, _II, _OI) [with _II = swig::PySequence_InputIterator<double, const swig::PySequence_Ref<double> >, _OI = double*, bool _BoolType = false]'
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algobase.h:317:   instantiated from '_OI std::__copy_aux(_II, _II, _OI) [with _II = swig::PySequence_InputIterator<double, const swig::PySequence_Ref<double> >, _OI = double*]'
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algobase.h:326:   instantiated from 'static _OI std::__copy_normal<<anonymous>, <anonymous> >::copy_n(_II, _II, _OI) [with _II = swig::PySequence_InputIterator<double, const swig::PySequence_Ref<double> >, _OI = double*, bool <anonymous> = false, bool <anonymous> = false]'
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algobase.h:387:   instantiated from '_OutputIterator std::copy(_InputIterator, _InputIterator, _OutputIterator) [with _InputIterator = swig::PySequence_InputIterator<double, const swig::PySequence_Ref<double> >, _OutputIterator = double*]'
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/vector.tcc:230:   instantiated from 'void std::vector<_Tp, _Alloc>::_M_assign_aux(_ForwardIterator, _ForwardIterator, std::forward_iterator_tag) [with _ForwardIterator = swig::PySequence_InputIterator<double, const swig::PySequence_Ref<double> >, _Tp = double, _Alloc = std::allocator<double>]'
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_vector.h:853:   instantiated from 'void std::vector<_Tp, _Alloc>::_M_assign_dispatch(_InputIterator, _InputIterator, __false_type) [with _InputIterator = swig::PySequence_InputIterator<double, const swig::PySequence_Ref<double> >, _Tp = double, _Alloc = std::allocator<double>]'
/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_vector.h:319:   instantiated from 'void std::vector<_Tp, _Alloc>::assign(_InputIterator, _InputIterator) [with _InputIterator = swig::PySequence_InputIterator<double, const swig::PySequence_Ref<double> >, _Tp = double, _Alloc = std::allocator<double>]'
functions_wrap.cxx:3801:   instantiated from 'void swig::assign(const PySeq&, Seq*) [with PySeq = swig::PySequence_Cont<double>, Seq = std::vector<double, std::allocator<double> >]'
functions_wrap.cxx:3816:   instantiated from 'static int swig::traits_asptr_stdseq<Seq, T>::asptr(PyObject*, Seq**) [with Seq = std::vector<double, std::allocator<double> >, T = double]'
functions_wrap.cxx:3878:   instantiated from 'static int swig::traits_asptr<std::vector<T, std::allocator<_CharT> > >::asptr(PyObject*, std::vector<T, std::allocator<_CharT> >**) [with T = double]'
functions_wrap.cxx:3030:   instantiated from 'int swig::asptr(PyObject*, Type**) [with Type = std::vector<double, std::allocator<double> >]'
functions_wrap.cxx:3152:   instantiated from 'static bool swig::traits_check<Type, swig::pointer_category>::check(PyObject*) [with Type = std::vector<double, std::allocator<double> >]'
functions_wrap.cxx:3159:   instantiated from 'bool swig::check(PyObject*) [with Type = std::vector<double, std::allocator<double> >]'
functions_wrap.cxx:3763:   instantiated from 'bool swig::PySequence_Cont<T>::check(bool) const [with T = std::vector<double, std::allocator<double> >]'
functions_wrap.cxx:3820:   instantiated from 'static int swig::traits_asptr_stdseq<Seq, T>::asptr(PyObject*, Seq**) [with Seq = std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >, T = std::vector<double, std::allocator<double> >]'
functions_wrap.cxx:3878:   instantiated from 'static int swig::traits_asptr<std::vector<T, std::allocator<_CharT> > >::asptr(PyObject*, std::vector<T, std::allocator<_CharT> >**) [with T = std::vector<double, std::allocator<double> >]'
functions_wrap.cxx:3030:   instantiated from 'int swig::asptr(PyObject*, Type**) [with Type = std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >]'
functions_wrap.cxx:4946:   instantiated from here
functions_wrap.cxx:3138: error: no type named 'category' in 'struct swig::traits<double>'

你忘记包含真正的错误了,只有它的前奏。提示是在于告诉你的句子还没有结束! - Lightness Races in Orbit
@tomalak 我已经添加了编译器输出的其余部分。 - JMD
2个回答

14

你未能导出内部向量类型。

%module functions
%{
#include "functions.h"
%}
%include "std_vector.i"
namespace std {
  %template(VecDouble) vector<double>;
  %template(VecVecdouble) vector< vector<double> >;
}

%include "functions.h"

顺便说一下,从SWIG生成-csharp输出对于创建包装器中的类型集合是一个非常好的检查方法。如果您不了解C#也没关系,只需查看创建的*.cs文件的名称即可。任何名为SWIGTYPE_*.cs的内容代表您没有包装的类型,可能会成为未来的问题。


谢谢。我会在周一回到办公室后尝试这个添加功能。 - JMD
刚刚意识到模板的顺序很重要。在内部向量之前对外部向量进行了建模,结果得到了与 OP 相同的错误。 - P.R.
@P.R. 你告诉 SWIG 的顺序非常微妙。如果你认为应该起作用的东西没有起作用,请尝试相反的顺序。这在导入许多其他 .i 文件的大型项目中尤其如此。 - lefticus

0

除了@lefticus的回复之外:

1.这里必须有一个空格:“double> >” %template(VecVecdouble) vector< vector >;

2.python应用程序代码:

nestedVec = functions.VecVecdouble()

# no matter push back tuple nor list, 
# nestedVec will automatically change it to tuple. 
nestedVec.push_back([1,3,5]) 
nestedVec.push_back((4,3,6))

# don't use a for loop to do nestedVec[i].clear()
# cause all items in nestedVec are tuple.
nestedVec.clear()

这只需要在旧版本的SWIG和C++编译器中使用。 - Mark Tolonen

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接