Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SWIG包装C++;对于Python:将字符串列表转换为STL字符串的STL向量 我想用SWIG来封装一个C++函数,它接受一个STL字符串的向量作为输入参数: #include <iostream> #include <string> #include <vector> using namespace std; void print_function(vector<string> strs) { for (unsigned int i=0; i < strs.size(); i++) cout << strs[i] << endl; }_C++_Python_String_Vector_Swig - Fatal编程技术网

SWIG包装C++;对于Python:将字符串列表转换为STL字符串的STL向量 我想用SWIG来封装一个C++函数,它接受一个STL字符串的向量作为输入参数: #include <iostream> #include <string> #include <vector> using namespace std; void print_function(vector<string> strs) { for (unsigned int i=0; i < strs.size(); i++) cout << strs[i] << endl; }

SWIG包装C++;对于Python:将字符串列表转换为STL字符串的STL向量 我想用SWIG来封装一个C++函数,它接受一个STL字符串的向量作为输入参数: #include <iostream> #include <string> #include <vector> using namespace std; void print_function(vector<string> strs) { for (unsigned int i=0; i < strs.size(); i++) cout << strs[i] << endl; },c++,python,string,vector,swig,C++,Python,String,Vector,Swig,当我用 from distutils.core import setup, Extension setup(name='mymod', version='0.1.0', description='test module', author='Craig', author_email='balh.org', packages=['mymod'], ext_modules=[Extension('mymod._mymod',

当我用

from distutils.core import setup, Extension

setup(name='mymod',
  version='0.1.0',
  description='test module',
  author='Craig',
  author_email='balh.org',
  packages=['mymod'],
  ext_modules=[Extension('mymod._mymod',
                         ['mymod/mymod.i'],
                         language='c++',
                         swig_opts=['-c++']),
                         ],
  )
然后导入并尝试运行,我得到以下错误:

Python 2.7.2 (default, Sep 19 2011, 11:18:13) 
[GCC 4.1.2 20080704 (Red Hat 4.1.2-48)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import mymod
>>> mymod.print_function("hello is seymour butts available".split())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: in method 'print_function', argument 1 of type 'std::vector<  std::string,std::allocator< std::string > >'
>>> 
Python 2.7.2(默认,2011年9月19日11:18:13)
linux2上的[GCC 4.1.2 20080704(Red Hat 4.1.2-48)]
有关详细信息,请键入“帮助”、“版权”、“信用证”或“许可证”。
>>>导入mymod
>>>mymod.print_函数(“hello is seymour butts available.”split()
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
TypeError:在方法“print_function”中,参数1的类型为“std::vector”
>>> 
<>我猜这是说SWIG没有提供一个默认的类型映射,用于在Python字符串的python列表和STL字符串的C++ STL向量之间进行转换。我觉得这是默认情况下他们可能会在某个地方提供的东西,但也许我不知道应该包含哪个文件。那我怎么才能让它工作呢


提前谢谢

SWIG确实支持将列表传递给以向量作为值或常量向量引用的函数。上的例子显示了这一点,我看不出你发布的内容有任何错误。还有别的问题;python找到的DLL不是最新的,标头中的using namespace std混淆了执行类型检查的SWIG包装器代码(请注意,.hpp中的“using namespace”语句通常是否定的,因为它将std中的所有内容都拉入全局名称空间),等等。

您需要告诉SWIG您想要一个向量字符串类型映射。它不会神奇地猜测所有可能存在的不同向量类型

这是Schollii提供的链接:

//To wrap with SWIG, you might write the following:

%module example
%{
#include "example.h"
%}

%include "std_vector.i"
%include "std_string.i"

// Instantiate templates used by example
namespace std {
   %template(IntVector) vector<int>;
   %template(DoubleVector) vector<double>;
   %template(StringVector) vector<string>;
   %template(ConstCharVector) vector<const char*>;
}

// Include the header file with above prototypes
%include "example.h"
//要使用SWIG包装,可以编写以下代码:
%模块示例
%{
#包括“example.h”
%}
%包括“标准向量i”
%包括“std_string.i”
//实例化示例使用的模板
名称空间标准{
%模板(IntVector)向量;
%模板(双向量)向量;
%模板(StringVector)向量;
%模板(ConstCharVector)向量;
}
//包含具有上述原型的头文件
%包括“example.h”

我使用名称空间std在有/没有
的情况下进行了尝试这没有什么区别。我同意头应该不使用
语句编写。我关心的是,你链接的例子是基本体的向量,比如int和double;我在网上找不到任何可以包装向量的东西,甚至向量。我的编辑地址是其他向量类型吗?如果不是的话,我错过了一些明显的东西。
//To wrap with SWIG, you might write the following:

%module example
%{
#include "example.h"
%}

%include "std_vector.i"
%include "std_string.i"

// Instantiate templates used by example
namespace std {
   %template(IntVector) vector<int>;
   %template(DoubleVector) vector<double>;
   %template(StringVector) vector<string>;
   %template(ConstCharVector) vector<const char*>;
}

// Include the header file with above prototypes
%include "example.h"