Swig 3.0.7,Python,std :: vector <class :: class>:包含std_vector.i在std :: vector

时间:2015-12-17 23:41:05

标签: python c++ vector swig

如何解决以下编译器错误:

假设:

%module SwigQuadKey

%{
#include "QuadKey/QuadKey.h"
%}

%include "operators.i"

%include <stdint.i>
%include <std_string.i>                           

%include "declspec.h"

%include "QuadKey/GeoCoordinate2d.h"              
%include "QuadKey/GeoCoordinateBoundingBox2d.h"   
%include "QuadKey/QuadKeyTypes.h"                 

%rename(GeoCoordinate2d) QuadKey::GeoCoordinate2d;

#if defined(SWIGPYTHON)                           
%include <std_vector.i>                           
%template(QuadKeyVector) std::vector<QuadKey::QuadKey>; 
#endif

%include "QuadKey/QuadKey.h"

或者

%module SwigQuadKey

%{
#include "QuadKey/QuadKey.h"
%}

%include "operators.i"

%include <stdint.i>
%include <std_string.i>                           

%include "declspec.h"

%include "QuadKey/GeoCoordinate2d.h"              
%include "QuadKey/GeoCoordinateBoundingBox2d.h"   
%include "QuadKey/QuadKeyTypes.h"                 

%rename(GeoCoordinate2d) QuadKey::GeoCoordinate2d;

%include "QuadKey/QuadKey.h"

#if defined(SWIGPYTHON)                           
%include <std_vector.i>                           
%template(QuadKeyVector) std::vector<QuadKey::QuadKey>; 
#endif

我在QuadKey命名空间内有一个名为QuadKey的C ++类,在编译Swig 3.0.7生成的.cpp文件时会产生以下编译器错误(无论我是否使用该模板,它都会执行此操作,仅包括std_vector .i将导致问题:

/home/mhoggan/Devel/QuadKeys/Swig/python/QuadKey_python.cpp: In 
function ‘PyObject* _wrap_QuadKey_getChildren(PyObject*, PyObject*)’:
/home/mhoggan/Devel/QuadKeys/Swig/python/QuadKey_python.cpp:10032:75: error: type/value mismatch at argument 1 in template parameter list for ‘template<class> class std::allocator’
   std::vector< QuadKey::QuadKey,std::allocator< QuadKey::QuadKey::QuadKey > > *arg2 = 0 ;
                                                                           ^
/home/mhoggan/Devel/QuadKeys/Swig/python/QuadKey_python.cpp:10032:75: error:   expected a type, got ‘QuadKey::QuadKey::QuadKey’
/home/mhoggan/Devel/QuadKeys/Swig/python/QuadKey_python.cpp:10032:77: error: template argument 2 is invalid
   std::vector< QuadKey::QuadKey,std::allocator< QuadKey::QuadKey::QuadKey > > *arg2 = 0 ;
                                                                             ^
/home/mhoggan/Devel/QuadKeys/Swig/python/QuadKey_python.cpp:10032:85: error: invalid type in declaration before ‘=’ token
   std::vector< QuadKey::QuadKey,std::allocator< QuadKey::QuadKey::QuadKey > > *arg2 = 0 ;
                                                                                     ^
/home/mhoggan/Devel/QuadKeys/Swig/python/QuadKey_python.cpp:10053:100: error: type/value mismatch at argument 1 in template parameter list for ‘template<class> class std::allocator’
   arg2 = reinterpret_cast< std::vector< QuadKey::QuadKey,std::allocator< QuadKey::QuadKey::QuadKey > > * >(argp2);
                                                                                                    ^
/home/mhoggan/Devel/QuadKeys/Swig/python/QuadKey_python.cpp:10053:100: error:   expected a type, got ‘QuadKey::QuadKey::QuadKey’
/home/mhoggan/Devel/QuadKeys/Swig/python/QuadKey_python.cpp:10053:102: error: template argument 2 is invalid
   arg2 = reinterpret_cast< std::vector< QuadKey::QuadKey,std::allocator< QuadKey::QuadKey::QuadKey > > * >(argp2);
                                                                                                      ^
/home/mhoggan/Devel/QuadKeys/Swig/python/QuadKey_python.cpp:10053:104: error: expected ‘>’ before ‘*’ token
   arg2 = reinterpret_cast< std::vector< QuadKey::QuadKey,std::allocator< QuadKey::QuadKey::QuadKey > > * >(argp2);
                                                                                                        ^
/home/mhoggan/Devel/QuadKeys/Swig/python/QuadKey_python.cpp:10053:104: error: expected ‘(’ before ‘*’ token
/home/mhoggan/Devel/QuadKeys/Swig/python/QuadKey_python.cpp:10053:106: error: expected primary-expression before ‘>’ token
   arg2 = reinterpret_cast< std::vector< QuadKey::QuadKey,std::allocator< QuadKey::QuadKey::QuadKey > > * >(argp2);
                                                                                                          ^
/home/mhoggan/Devel/QuadKeys/Swig/python/QuadKey_python.cpp:10053:114: error: expected ‘)’ before ‘;’ token
   arg2 = reinterpret_cast< std::vector< QuadKey::QuadKey,std::allocator< QuadKey::QuadKey::QuadKey > > * >(argp2);
...

请注意,在为c#或java生成代码时,此模板可以正常工作。它只会破坏Python。

2 个答案:

答案 0 :(得分:0)

那是因为您在public boolean hasInternetConnectivity() { try { // connect to google on port 80, the HTTP port Socket s = new Socket("www.google.com", 80); // the above would have thrown if failed, so we are good s.close(); return true; } catch (Exception e) { // check logcat to see why it failed, you could then catch and handle each exception independently ( time out, host unknown, end of stream, etc.. ) e.printStackTrace(); // the connection has failed, return false return false; } 中定义了%template。尝试

namespace std

答案 1 :(得分:0)

解决方案是将命名空间重命名为QuadKeys。所以现在:

%template(QuadKeyVector) std::vector<QuadKeys::QuadKey>;

作品。