title: SWIG c++ to python : vector problems
body: <p>I am following the documentation here: <a href="http://www.swig.org/Doc3.0/Library.html#Library_stl_cpp_library" rel="nofollow noreferrer">http://www.swig.org/Doc3.0/Library.html#Library_stl_cpp_library</a> to write a wrapper to a simple example code involving vectors.</p>
  
  <p>Here is the header file:</p>
  
  <pre><code>### word.h ###
  #include&lt;string&gt;
  #include&lt;vector&gt;
  
  class Word{
  public:
      Word(std::string word, int numWords, std::vector&lt;double&gt; &amp;values);
      ~Word();
  
      void updateWord(std::string newWord);
      std::string getWord();
      void processValues();
  
  private:
      std::string theWord;
      int totalWords;
      std::vector&lt;double&gt; values;
  };
  </code></pre>
  
  <p>And the source file:</p>
  
  <pre><code>### word.cpp ###
  #include "word.h"
  #include &lt;cfloat&gt;
  #include &lt;cmath&gt;
  #include &lt;cstdlib&gt;
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;time.h&gt;
  
  Word::Word(std::string word, int numWords, std::vector&lt;double&gt; &amp;values) :
      theWord(word), totalWords(numWords), values(values){
      // TODO: constructor
  }
  
  Word::~Word() {
      // TODO: destructor
  }
  
  void Word::updateWord(std::string newWord) {
      this-&gt;theWord = newWord;
  }
  
  std::string Word::getWord() {
      return this-&gt;theWord;
  }
  
  void Word::processValues() {
      values.resize(totalWords);
      // do something with values here
  }
  
  /*  
      rest of the code that uses the other imports
  */
  </code></pre>
  
  <p>Here is the interface file:</p>
  
  <pre><code>### word.i ###
  %module word
  %{
  #include "word.h"
  %}
  
  %include "std_string.i"
  %include "std_vector.i"
  
  namespace std {
      %template(vectord) vector&lt;double&gt;;
  }
  
  %include "word.h"
  </code></pre>
  
  <p>My compilation steps are as follows:</p>
  
  <pre><code>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++
  </code></pre>
  
  <p>The compilation goes through without any errors. However, on trying to create the object in Python I get the following error:</p>
  
  <pre><code>In [1]: import word
  
  In [2]: w = word.Word('test', 10, [10.2])
  ---------------------------------------------------------------------------
  TypeError                                 Traceback (most recent call last)
  &lt;ipython-input-2-ee2e5c406fd9&gt; in &lt;module&gt;()
  ----&gt; 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):
  --&gt; 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&lt; double,std::allocator&lt; double &gt; &gt; &amp;'
  </code></pre>
  
  <p>A bit of searching online leads me to believe that using the template in the SWIG definition solves this problem. </p>
  
  <p>However, in my case it hasn't. Could you please point me in the right direction?</p>