[C++-sig] Re: operator[]
Martin Casado
casado2 at llnl.gov
Wed May 22 18:50:37 CEST 2002
> I am not sure how to fix the following compiler problem...
>
> ------------------------------------------------
> #include <boost/python/module.hpp>
> #include <boost/python/class.hpp>
> using namespace boost::python;
>
> class Vector2d {
> public:
> Vector2d():x(0.0),y(0.0) {}
> Vector2d(const double xval, const double yval): x(xval),y(yval) {}
> ~Vector2d() {}
> double& operator[](const int i) {return i==0 ? x : y;}
>
> protected:
> double x, y;
> };
>
> BOOST_PYTHON_MODULE_INIT(simpleVector2)
> {
> module Vect2dMod("simpleVector2");
> Vect2dMod
> .add(
> class_<Vector2d>("Vector2d")
> .def_init()
> .def_init(args<const double, const double>())
> //.def("__getitem__", &Vector2d::operator[])
> .def("__getitem__", (double& (Vector2d::*)(const int))
> &Vector2d::operator[])
> )
> ;
> }
> -------------------------------------------------
>
> Problem:
> If I replace the class definition of operator[] in
> double& operator[](int i) const {return i==0 ? x : y;}
> by either
> .def("__getitem__", &Vector2d::operator[])
> or
> .def("__getitem__", (double (Vector2d::*)(const int) const)
> &Vector2d::operator[])
> it compiled, but the above code issued error message:
>
> "/usr/dnta/kull/developers/thirdPartySoftware/boost-chuzo/boost/boost/pytho
>n/preprocessed/returning_non_void.hpp", line 35: error:
> class
>
> "boost::python::detail::specify_a_result_policy_to_wrap_functions_ret
> urning<double &>" has no member "convertible"
> if(!cr.convertible())return 0;
>
> AND
>
> "/usr/dnta/kull/developers/thirdPartySoftware/boost-chuzo/boost/boost/pytho
>n/preprocessed/returning_non_void.hpp", line 37: error:
> expression must have (pointer-to-) function type
> PyObject*result=cr(((
>
> ???
> Does anyone have any idea how to fix this?
Chuzo,
When returning an internal reference you must specify a return policy,
such as return_internal_reference<>(). The compiler error gives
you a hint to this in the second declaration..
"boost::python::detail::specify_a_result_policy_to_wrap_functions_ret
urning<double &>" has no member "convertible"
if(!cr.convertible())return 0;
Note the "specify_a_result_policy_to_wrap_functions_ret"
:-)
~~m
More information about the Cplusplus-sig
mailing list