[C++-sig] Continuing Pointer Issue
CLARKE,RON (HP-Vancouver,ex1)
ron_clarke at hp.com
Mon Jan 13 22:28:01 CET 2003
David - thanks for the response on the previous message regarding wrapping a
pointer member variable. (see below)
class car
> {
> public:
> car(void);
> virtual ~car(void);
> private:
> ...
> };
>
> class garage{
> public:
> garage(void);
> ~garage(void);
> car* pCar;
> };
>
> Class garage contains a public pointer to a car object (I don't have the
> option of changing the C++ code; it would negatively affect too many
users).
> I can create garage and car objects OK and Python understands each one:
> c=car()
> g=garage()
>
> However, if I try something like this:
> g.pCar = car()
> the resulting object is null and trying to simply print the object yields
a
> "TypeError: bad argument for built-in operation".
>
> What have I missed? Can someone please provide an example (other than what
> is in the documentation)?
I think this might work for you:
class_<garage>("garage")
.add_property(
make_getter(&garage::pCar, with_custodian_and_ward<1,2>())
, make_setter(&garage::pCar, return_internal_reference<>()))
However, if garage::~garage() does "delete pCar" you're going to have
big problems. Let me know if that's the case and we can try something
else.
------(end of response) ----------
I tried the code as specified, but it did not compile. So based on the
example, I did some more reading of the documentation and source code, and
tried to figure something out, but I'm getting nowhere. I cannot get the
code (or several variations on the them) to compile, much less test
successfully in Python. Here is the code that fails to compile:
BOOST_PYTHON_MODULE(house)
{
class_<garage>("garage")
.add_property("pCar"
, make_getter(&garage::pCar, with_custodian_and_ward<1,2>())
, make_setter(&garage::pCar, return_internal_reference<>() )
)
;
}
The Visual Studio .NET compiler emits these messages:
f:\Downloads\Python
etc\Boost2\boost_1_29_0\boost\python\data_members.hpp(34) : error C2079:
'cr' uses undefined struct
'boost::python::detail::specify_a_result_policy_to_wrap_functions_returning<
T>'
with
[
T=source
]
f:\Downloads\Python
etc\Boost2\boost_1_29_0\boost\python\data_members.hpp(27) : while compiling
class-template member function 'PyObject
*boost::python::detail::member<Data,Class,Policies>::get(Data garage::*
,PyObject *,PyObject *,const
boost::python::with_custodian_and_ward<custodian,ward,BasePolicy_> &)'
with
[
Data=car *,
Class=garage,
Policies=boost::python::with_custodian_and_ward<1,2,boost::python::default_c
all_policies>,
custodian=1,
ward=2,
BasePolicy_=boost::python::default_call_policies
]
f:\Downloads\Python
etc\Boost2\boost_1_29_0\boost\python\data_members.hpp(83) : see reference to
class template instantiation
'boost::python::detail::member<Data,Class,Policies>' being compiled
with
[
Data=car *,
Class=garage,
Policies=boost::python::with_custodian_and_ward<1,2,boost::python::default_c
all_policies>
]
f:\boost_tests\simple1\house_boostwrapper\house_boostwrapper.cpp(48)
: see reference to function template instantiation
'boost::python::api::object boost::python::make_getter(D garage::* ,const
boost::python::with_custodian_and_ward<custodian,ward,BasePolicy_> &)' being
compiled
with
[
D=car *,
custodian=1,
ward=2,
BasePolicy_=boost::python::default_call_policies
]
f:\Downloads\Python
etc\Boost2\boost_1_29_0\boost\python\data_members.hpp(35) : error C2228:
left of '.convertible' must have class/struct/union type
f:\Downloads\Python
etc\Boost2\boost_1_29_0\boost\python\data_members.hpp(39) : error C2064:
term does not evaluate to a function
---- (end compiler output) ----
The error message implies I need a result policy, but I cannot find anything
in the documentation that says what a result policy is. Is it the same as a
Call Policy? If so, isn't that what I have in the code being compiled (re:
with_custodian_and_ward and return_internal_reference)?
I would like to be able to do this in Python:
from vehicles import car
from house import *
from parkinglot import *
g = garage() #in house module; garage contains car pointer allocated in
garage constructor
Print "the color of the car is %s", g.pCar.getColor()
pl = parkingLot()
pl.parkTheCar(g.pCar)
Is there a way to make this work in Boost.Python?
Thanks in advance for the assistance.
-Ron
_________________________________________
Ron Clarke
Digital Printing Technologies
Hewlett-Packard Company
ron_clarke at hp.com
360-212-0193
More information about the Cplusplus-sig
mailing list