List Info

Thread: access struct inside class




access struct inside class
country flaguser name
Sweden
2007-10-14 09:55:29
Hello,
im trying to get python to read and write to a struct inside
a class
in the same way i do in c++
Customers hej;
hej.address.contact = "The Devil";

but i cant even get the code to compile, any suggestions?

#include <iostream>
#include <string>

using namespace std;
class Customers {
private:
struct s_address
{
string contact;
string street1;
string street2;
string city;
string province;
string postalCode;
string country;
};
public:
s_address address;
Customers(){}
~Customers(){}
};

#include <boost/python.hpp>
#include <boost/python/module.hpp>
#include <boost/python/class.hpp>
#include <boost/python/scope.hpp>
#include <boost/python/def.hpp>

using namespace boost::python;

BOOST_PYTHON_MODULE(test)
{
scope outer=
class_<Customers>("Customers")
;
class_<Customers::address>("address")
.def_readwrite("contact",
&Customers::address.contact)
;
}

When i compile i get this error:
test.cpp: In function ‘void init_module_test()’:
test.cpp:36: error: ‘Customers::address’ cannot appear in a

constant-expression
test.cpp:36: error: template argument 1 is invalid
test.cpp:18: error: invalid use of non-static data member 
‘Customers::address’
test.cpp:37: error: from this location

what i want to be able to do in python is
from test import *
cust = Customers()
cust.address.contact = "Hello"
print cust.address.contact
_______________________________________________
C++-sig mailing list
C++-sigpython.org
http:
//mail.python.org/mailman/listinfo/c++-sig

Re: access struct inside class
country flaguser name
France
2007-10-15 12:09:53
Hello,

it should be

&Customers::address::contact

not

&Customers::address.contact

...
_______________________________________________
C++-sig mailing list
C++-sigpython.org
http:
//mail.python.org/mailman/listinfo/c++-sig

Re: access struct inside class
country flaguser name
Sweden
2007-10-15 13:48:53
Hello

Thanks for ypur replay
whit your proposed change i get:

test.cpp: In function ‘void init_module_test()’:
test.cpp:36: error: ‘Customers::address’ cannot appear in a

constant-expression
test.cpp:36: error: template argument 1 is invalid
test.cpp:37: error: ‘class Customers::address’ is not a
class or namespace
error: command 'gcc' failed with exit status 1
make: *** [default] Fel 1

one error down 3 more to go

Nicolas Lelong wrote:
> Hello,
>
> it should be
>
> &Customers::address::contact
>
> not
>
> &Customers::address.contact
>
> ...
> _______________________________________________
> C++-sig mailing list
> C++-sigpython.org
> http:
//mail.python.org/mailman/listinfo/c++-sig
>
>   

_______________________________________________
C++-sig mailing list
C++-sigpython.org
http:
//mail.python.org/mailman/listinfo/c++-sig

Re: access struct inside class
user name
2007-10-16 05:21:31

On 10/14/07, Simon Norberg < simondackbrann.net">simondackbrann.net> wrote:
Hello,
im trying to get python to read and write to a struct inside a class
in the same way i do in c++
Customers hej;
hej.address.contact = "The Devil";;

but i cant even get the code to compile, any suggestions?

#include <iostream>
#include <string>

using namespace std;
class Customers {
private:
struct s_address
{
string contact;
string street1;
string street2;
string city;
string province;
string postalCode;
string country;
};
public:
s_address address;
Customers(){}
~Customers(){}
};

1. you cannot export variable, without exporting its type first: you have to expose s_address class too, if you want to expose address mem. variable.

2. here is the code that was generated by Py++( http://language-binding.net/ ), the only change I did for original code was to change everything to public.

#include "boost/python.hpp"

#include "1.hpp"

namespace bp = boost::python;

BOOST_PYTHON_MODULE(pyplusplus){
 ; &nbsp; { //::Customers
 &nbsp;   ; &nbsp; typedef bp::class_< Customers > Customers_exposer_t;
  ; &nbsp; &nbsp;  Customers_exposer_t Customers_exposer = Customers_exposer_t( "Customers" );
 ; &nbsp; &nbsp; &nbsp; bp::scope Customers_scope( Customers_exposer );
 &nbsp;   ; &nbsp; bp::class_< Customers::s_address >( "s_address" ) &nbsp; 
 &nbsp; &nbsp; &nbsp;   ; &nbsp; .def_readwrite( "city", &Customers::s_address::city ) &nbsp; 
 &nbsp;   ; &nbsp; &nbsp; &nbsp; .def_readwrite( "contact", &Customers::s_address::contact ) &nbsp; 
 &nbsp; &nbsp; &nbsp;   ; &nbsp; .def_readwrite( "country", &Customers::s_address::country ) &nbsp; 
 &nbsp; &nbsp; &nbsp;   ; &nbsp; .def_readwrite( "postalCode";, &Customers::s_address::postalCode ) &nbsp; 
 &nbsp;   ; &nbsp; &nbsp; &nbsp; .def_readwrite( "province", &Customers::s_address::province ) &nbsp; 
 &nbsp; &nbsp; &nbsp;   ; &nbsp; .def_readwrite( "street1", &Customers::s_address::street1 ) &nbsp; 
 &nbsp; &nbsp; &nbsp;   ; &nbsp; .def_readwrite( "street2", &Customers::s_address::street2 );
 ; &nbsp; &nbsp; &nbsp; Customers_exposer.def( bp::init&lt; >() );
 &nbsp;   ; &nbsp; Customers_exposer.def_readonly( "address", &Customers::address );
 &nbsp;  }
}


--
Roman Yakovenko
C++ Python language binding
http://www.language-binding.net/
[1-4]

about | contact  Other archives ( Real Estate discussion Medical topics )