[C++-sig] Re: operator[]

chuzo okuda okuda1 at llnl.gov
Wed May 22 19:21:56 CEST 2002


Greeting Martin,
You are right, but I got different error this time...

I added:

#include <boost/python/return_internal_reference.hpp>
and added
return_internal_reference<>()
to the line becoming: 
.def("__getitem__", (double& (Vector2d::*)(const
int))&Vector2d::operator[],return_internal_reference<>())

and I get:

"/usr/dnta/kull/developers/thirdPartySoftware/boost-chuzo/boost/boost/python/to_python_indirect.hpp",
line 66: error:
          incomplete type is not allowed
            BOOST_STATIC_ASSERT(is_class<T>::value);

:-(
Chuzo



Martin Casado wrote:
> 
> > 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
> 
> _______________________________________________
> C++-sig mailing list
> C++-sig at python.org
> http://mail.python.org/mailman/listinfo/c++-sig





More information about the Cplusplus-sig mailing list