$include_dir="/home/hyper-archives/boost/include"; include("$include_dir/msg-header.inc") ?>
From: David Abrahams (david.abrahams_at_[hidden])
Date: 2001-07-26 11:26:19
This sample code reveals your problem:
    void f(vector const& x, vector const& y) { x + y };
It won't compile either, for the same reasons.
your operator+ and operator- should be const member functions. Try adding
const before the opening brace of each one.
-Dave
----- Original Message -----
From: <jfo60540_at_[hidden]>
To: <boost_at_[hidden]>
Sent: Thursday, July 26, 2001 11:55 AM
Subject: [boost] Operator overloading problems with Python 2.1 and gcc 3.0
> Hi -
>
> I am new to boost.python. I compiled the library with Python 2.1.1 and
> gcc
> 3.0 under Solaris 2.7. There were a few minor problems (gcc stl
> headers
> are broken and std::sin(float) and std::cos(float) are left undefined
> ),
> but I eventually  got all the tests (in particular comprehensive.py)
> to
> work properly. I then tried the following code to test
> the operator overloading feature:
>
>
> #include <iostream>
>
> class vector {
>      protected:
>          int x;
>          int y;
>      public:
>        vector(int a=0,int b=0) {
>          x = a;
>          y = b;}
>        void set_values(int a, int b) {
>          x=a;
>          y=b;}
>        vector operator+ (const vector operand) {
>          vector z;
>          z.x = x + operand.x;
>          z.y = y + operand.y;
>          return z;}
>        vector operator- (const vector operand) {
>          vector z;
>          z.x = x - operand.x;
>          z.y = y - operand.y;
>          return z;}
> };
>
> #include <boost/python/class_builder.hpp>
> namespace python = boost::python;
> BOOST_PYTHON_MODULE_INIT(over)
> {
>   try
>   {
>     python::module_builder over("over");
>     python::class_builder<vector> vector_class(over, "vector");
>     vector_class.def(python::constructor<int,int>());
>     vector_class.def(&vector::set_values, "set_values");
>     vector_class.def(boost::python::operators<(boost::python::op_add |
> boost::python::op_sub)>());
>   }
>   catch(...)
>   {
>     python::handle_exception(); // Deal with the exception for Python
>   }
> }
>
>
> Compilation of the above fails with the following error messages:
>
> /usr/local/ap/include/boost/python/operators.hpp: In member function
> `PyObject*
>    boost::python::detail::define_operator<2>::operator_function<Left,
>    Right>::do_call(PyObject*, PyObject*) const [with Left = const
> vector&,
>    Right = const vector&]':
> /usr/local/ap/include/boost/python/detail/extension_class.hpp:861:
> instantiated from here
> /usr/local/ap/include/boost/python/operators.hpp:295: passing `const
> vector' as
>    `this' argument of `vector vector::operator-(vector)' discards
> qualifiers
> /usr/local/ap/include/boost/python/operators.hpp: In member function
> `PyObject*
>    boost::python::detail::define_operator<1>::operator_function<Left,
>    Right>::do_call(PyObject*, PyObject*) const [with Left = const
> vector&,
>    Right = const vector&]':
> /usr/local/ap/include/boost/python/detail/extension_class.hpp:861:
> instantiated from here
> /usr/local/ap/include/boost/python/operators.hpp:294: passing `const
> vector' as
>    `this' argument of `vector vector::operator+(vector)' discards
> qualifiers
>
>
> Any help would be appreciated.
>
>
>
> Info: http://www.boost.org  Unsubscribe:
<mailto:boost-unsubscribe_at_[hidden]>
>
> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
>
>