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

Matthew Scouten matthew.scouten at gmail.com
Tue Oct 16 19:31:40 CEST 2007


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();
};

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?



More information about the Cplusplus-sig mailing list