How to use a Python list to assign a std::vector in C++ using SWIG?

久未见 提交于 2019-12-12 08:06:48

问题


I have a simple C++ class that contains a std::vector member and a member function that takes a std::vector as an argument that I am wrapping with SWIG and calling from Python. The example code is below.

After compiling it, I go into Python and do:

import test
t = test.Test()
a = [1, 2, 3]
b = t.times2(a) # works fine
t.data = a # fails!

The error message I get is:

TypeError: in method 'Test_data_set', argument 2 of type 'std::vector< double,std::allocator< double > > *'

I know that I can just do:

t.data = test.VectorDouble([1,2,3])

But I would like to know how to just use a Python list directly in the assignment, or at least understand why it doesn't work.


Here's the example code.

test.i:

%module test

%include "std_vector.i"

namespace std {
    %template(VectorDouble) vector<double>;
};

%{
#include "test.hh"
%}

%include "test.hh"

test.hh:

#include <vector>

class Test {
    public:
        std::vector<double> data;
        std::vector<double> times2(std::vector<double>);
};

test.cc:

#include "test.hh"

std::vector<double>
Test::times2(
    std::vector<double> a)
{
    for(int i = 0; i < a.size(); ++i) {
        a[i] *= 2.0;
    }
    return a;
}

makefile:

_test.so: test.cc test.hh test.i
    swig -python -c++ test.i
    g++ -fpic -shared -o _test.so test.cc test_wrap.cxx -I/opt/local/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -L/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/config/ -lpython2.7

回答1:


Try using the %naturalvar directive on the Test::data member. In your test.i file:

%naturalvar Test::data;
%include "test.hh"

As described in the SWIG documentation on C and C++ members, SWIG will default to accessing nested structs and classes via pointers. The %naturalvar directs the interface to be accessed by value rather than by reference.




回答2:


Have a look at the typemaps example chapter from the SWIG documentation: http://www.swig.org/Doc2.0/SWIGDocumentation.html#Typemaps_nn40 (at the end of the example where it discusses struct access).

You probably need to add a memberin typemap for your data member, and maybe out and in as well, if those are not already provided by SWIGs std_vector.i.



来源:https://stackoverflow.com/questions/22491921/how-to-use-a-python-list-to-assign-a-stdvector-in-c-using-swig

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!