矢量&lt; vector <double>&gt; swig和python的参数</double>

时间:2011-03-10 16:15:09

标签: c++ python swig

我正在试图弄清楚如何使用SWIG来包装一个将2d向量返回给python的c ++函数。我有文件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>'

2 个答案:

答案 0 :(得分:9)

您也无法导出内部矢量类型。

%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的东西都代表了你没有包装的类型,以及潜在的未来问题。

答案 1 :(得分:0)

除了@lefticus的回复:

1.此处必须有空格:&#34; double&gt; &GT;&#34;   %template(VecVecdouble)vector&lt; vector&gt ;;

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()
相关问题