SWIG c++到python:向量问题
SWIG c++ to python : vector problems
我在这里遵循文档:http://www.swig.org/Doc3.0/Library.html#Library_stl_cpp_library为涉及向量的简单示例代码编写包装器。
这是头文件:
### word.h ###
#include<string>
#include<vector>
class Word{
public:
Word(std::string word, int numWords, std::vector<double> &values);
~Word();
void updateWord(std::string newWord);
std::string getWord();
void processValues();
private:
std::string theWord;
int totalWords;
std::vector<double> values;
};
和源文件:
### word.cpp ###
#include "word.h"
#include <cfloat>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <time.h>
Word::Word(std::string word, int numWords, std::vector<double> &values) :
theWord(word), totalWords(numWords), values(values){
// TODO: constructor
}
Word::~Word() {
// TODO: destructor
}
void Word::updateWord(std::string newWord) {
this->theWord = newWord;
}
std::string Word::getWord() {
return this->theWord;
}
void Word::processValues() {
values.resize(totalWords);
// do something with values here
}
/*
rest of the code that uses the other imports
*/
这是接口文件:
### word.i ###
%module word
%{
#include "word.h"
%}
%include "std_string.i"
%include "std_vector.i"
namespace std {
%template(vectord) vector<double>;
}
%include "word.h"
我的编译步骤如下:
swig -c++ -python word.i
g++ -c -fpic word.cpp word_wrap.cxx -I/usr/include/python2.7
g++ -shared word.o word_wrap.o -o _word.so -lstdc++
编译过程中没有任何错误。然而,在尝试用Python创建对象时,我得到了以下错误:
In [1]: import word
In [2]: w = word.Word('test', 10, [10.2])
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-2-ee2e5c406fd9> in <module>()
----> 1 w = word.Word('test', 10, [10.2])
/home/anarayan/workspace/swig-learn/word.pyc in __init__(self, word, numWords, values)
276
277 def __init__(self, word, numWords, values):
--> 278 this = _word.new_Word(word, numWords, values)
279 try:
280 self.this.append(this)
TypeError: in method 'new_Word', argument 3 of type 'std::vector< double,std::allocator< double > > &'
通过在线搜索,我相信在SWIG定义中使用模板可以解决这个问题。
然而,就我而言,它并没有。你能告诉我正确的方向吗?
它不起作用,因为您通过引用传递向量。如果您通过值或常量引用传递,SWIG知道该做什么并生成正确的代码。简单地更改声明和定义中的类型
Word(std::string word, int numWords, std::vector<double> const &values);
就足够了。
$ swig -c++ -python word.i
$ g++ -c -fpic word.cpp word_wrap.cxx -I/usr/include/python2.7
$ g++ -shared word.o word_wrap.o -o _word.so -lstdc++
$ python
Python 2.7.13 (default, Nov 24 2017, 17:33:09)
[GCC 6.3.0 20170516] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import word
>>> w = word.Word('test', 10, [10.2])
在这种情况下,您可以应用上述调整,因为您不需要values
作为纯粹的参考。如果需要引用,则需要更多的工作,并且您必须编写自己的类型映射(可能还有自己的容器(。
相关文章:
- 如何在C++向量中奇数元素前面加上值-1,我在使用insert函数时遇到了问题
- 添加存储在向量中的大整数的函数出现问题
- C++ 继承向量问题(无限循环+在其他类中使用向量的问题)
- SWIG c++到python:向量问题
- 向量问题,如何删除向量中的数组?
- 向量问题,也许有点手
- ndk中的标准与cpp向量问题
- 从数据文件到多维向量问题
- C++:向量问题的向量
- 类向量 c++ 问题
- 特征向量问题
- C++std::向量问题
- 结构中的向量问题
- C++类向量问题
- 向量的向量问题
- 获取索引和元素-向量问题
- 搜索或发现向量问题内部
- C++向量问题;通过引用传递内容
- 几个c++向量问题
- 指向包含向量问题的实例类的指针<int>