SWIG c++;python:向量问题

SWIG c++;python:向量问题,python,c++,swig,Python,C++,Swig,我遵循这里的文档:编写一个包含向量的简单示例代码的包装器 以下是头文件: ### word.h ### #include<string> #include<vector> class Word{ public: Word(std::string word, int numWords, std::vector<double> &values); ~Word(); void updateWord(std::string newW

我遵循这里的文档:编写一个包含向量的简单示例代码的包装器

以下是头文件:

### 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;
};
编译过程中没有任何错误。但是,在尝试用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 > > &'
[1]中的
:导入单词
在[2]中,w=word.word('test',10[10.2])
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
在()
---->1 w=word.word('test',10,[10.2])
/home/anarayan/workspace/swig learn/word.pyc in_uuuuinit_uuu(self、word、numWords、values)
276
277定义初始值(自我、单词、numWords、值):
-->278 this=\u word.new\u word(word、numWords、values)
279试试:
280 self.this.append(this)
类型错误:在方法“new_Word”中,参数3的类型为“std::vector>&”
在线搜索让我相信在SWIG定义中使用模板可以解决这个问题


然而,在我的情况下,它没有。你能给我指出正确的方向吗?

它不起作用,因为你通过引用传递向量。如果改为按值传递或按常量引用传递,SWIG知道该做什么并生成正确的代码。只需更改的声明和定义中的类型

Word(std::string word, int numWords, std::vector<double> const &values);

在这种情况下,您可以应用上述调整,因为您不需要将
作为纯参考。如果需要引用,则需要更多的工作,并且必须编写自己的类型映射(并且可能是您自己的容器)。

如果我真的想使用(和修改)一个STD::vector在我的C++函数中而不复制,我会有什么选择?指向std::vector的原始指针?共享ptr到std::vector?(我也在问)@ GabrielDevillers,你不能通过C++向量查看Python列表,因为按照定义,向量拥有它的内存。如果使用NumPy数组而不是列表,则可以使用
NumPy.i
SWIG绑定获取指向基础数组的指针,并对其进行适当修改:
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++
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 > > &'
Word(std::string word, int numWords, std::vector<double> const &values);