[C++-sig] passing a vector of structs

Roman Yakovenko roman.yakovenko at gmail.com
Tue Oct 16 20:48:45 CEST 2007


On 10/16/07, Matthew Scouten <matthew.scouten at gmail.com> wrote:
>
> I have a vector of 'struct foo' in c++ that needs to be passed to a
> function python.
>
> 'struct foo' is an extremely simple structure containing 3 ints and a
> trivial ctor.
>
> so I create a foo_wrapper:
>
> struct foo_wrapper : foo , wrapper<foo>
> {
>         foo_wrapper(const foo& f);
>         foo_wrapper();
> };


In this case you don't need to create wrapper

and export it:
>
> class_<foo_wrapper>("foo")
>                 .def(init<>())
>                 .def_readwrite("iE", &foo::iE )
>                 .def_readwrite("bA", &foo::bA)
>                 .def_readwrite("bL", &foo::bL)
>                 ;
>
> next I export a vector of foo_wrappers:
>
> class_< std::vector<foo_wrapper> >("VectorOfFoo")
>                 .def(vector_indexing_suite< std::vector<foo_wrapper>, true
> >() );
>
> I pass the vector<foo> to a python function:
>
> Traceback (most recent call last):
>   File "C:\redacted", line 23, in <module>
>     pc.RequestExchangeList()
> TypeError: No to_python (by-value) converter found for C++ type: class
> std::vector<struct foo ,class std::allocator<struct foo > >
>
> I try to copy the elements to a new vector<foo_wrapper>, it seems to
> work fine, untill I try to access an element of that vector and get:
>
> Traceback (most recent call last):
>   File "<stdin>", line 1, in <module>
>   File "<stdin>", line 7, in OnExchangeList
> TypeError: No to_python (by-value) converter found for C++ type:
> struct TTExchangeStruct_wrapper
>
> So what am I doing wrong? How am I supposed to pass a vector of
> structs from c++ to python?



You need to post small and complete example.

-- 
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/cplusplus-sig/attachments/20071016/bec249c4/attachment.htm>


More information about the Cplusplus-sig mailing list