From: Christoph Schmidt-H. <chr...@gm...> - 2011-10-06 10:15:35
|
On Thu, Oct 06, 2011 at 07:41:43AM +0100, William S Fulton wrote: > On 26/09/11 12:18, Christoph Schmidt-Hieber wrote: > >Hi, > > > >I'm trying to access a private member of a C++ class as an attribute of the corresponding Python class along the lines suggested here: > > > >http://www.swig.org/Doc1.3/SWIG.html#SWIG_adding_member_functions > > > >The goal is obviously to avoid non-Pythonic get/set functions while keeping the member private in C++. In brief, my C++ class looks like this: > > > >class Person { > >public: > > > > /* ... */ > > > > void set_age(double val) {age=val;} > > double get_age() const {return age;} > > > >private: > > double age; > > > > /* ... */ > > > >}; > > > >In the swig interface file, I'm doing this, as suggested in the 1.3 documentation: > > > >class Person { > > > > /* ... */ > > > > %extend { > > double age; > > } > > > > /* ... */ > > > >}; > > > >%{ > > double Person_age_get(Person *p) { > > return p->get_age(); > > } > > void Person_age_set(Person *p, double val) { > > p->set_age(val); > > } > >} > > > >While this code works fine in swig 2.0.4, it results in an error message with swig 1.3 because the set_ function is supposed to return a pointer of some sort; e.g.: > > > >error: could not convert ‘Person_age_set(arg1, arg2)’ to ‘bool’ > >error: type ‘void’ argument given to ‘delete’, expected pointer > > > >How can I get this to run with both 1.3 and 2.0? > > I can't replicate this with 1.3.29 or later, I'm not sure which > version you are using. But in any case, you can deal with different > SWIG versions using the preprocessor and the SWIG_VERSION, eg: > > #if SWIG_VERSION<0x020000 > // swig 1.3.x > #else > // swig 2.0.0 and later > #endif > > William Thanks for your reply. I apologise for the incomplete example - turns out that this problem only appears with char*, but not with double. I'm now resorting to using std::string throughout, which seems to work with both 1.3 and 2.0. Cheers, Christoph |