[C++-sig] access struct inside class

Simon Norberg simon at dackbrann.net
Sun Oct 14 16:55:29 CEST 2007


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



More information about the Cplusplus-sig mailing list