[C++-sig] .def( bp::self == bp::self ); causes compile problems for Py++ generated wrapper

William Ladwig wladwig at wdtinc.com
Fri Sep 19 23:37:09 CEST 2008


If you can change the C++ code, try declaring the operator== argument as a const reference (should probably do this anyhow since you aren't modifying the object):

bool operator==(const MyFrac& b) const {
        return numerator == b.numerator && denominator == b.denominator;}


Hope this helps,
Bill

-----Original Message-----
From: c++-sig-bounces+wladwig=wdtinc.com at python.org [mailto:c++-sig-bounces+wladwig=wdtinc.com at python.org] On Behalf Of Arne Grimstrup
Sent: Friday, September 19, 2008 3:38 PM
To: c++-sig at python.org
Subject: [C++-sig] .def( bp::self == bp::self ); causes compile problems for Py++ generated wrapper

I'm having some trouble with Py++ generating a wrapper for a class
that defines a == operator.  The class definition looks like this:

class MyFrac {
   private:
     int numerator;
     int denominator;
     friend std::ostream& operator<<(std::ostream&, MyFrac&);

   public:
     MyFrac(void) : numerator(1), denominator(1) {}
     MyFrac(int n, int d) : numerator(n), denominator(d) {}
     bool operator==(MyFrac& b) const {
        return numerator == b.numerator && denominator == b.denominator;
     }
};

When the code is generated using pyplusplus_gui, the resulting C++ bindings
look like this:

#include "boost/python.hpp"

#include "MyFrac.h"

namespace bp = boost::python;

BOOST_PYTHON_MODULE(MyFrac){
     bp::class_< MyFrac >( "MyFrac" )
         .def( bp::init< >() )
         .def( bp::init< int, int >(( bp::arg("n"), bp::arg("d") )) )
         .def( bp::self == bp::self );
}

Attempting to compile the bindings gives:

operators.hpp:226: error: no match for 'operator==' in 'l == r'
MyFrac.h:14: note: candidates are: bool MyFrac::operator==(MyFrac&) const
function/function_base.hpp:577: note:
    bool boost::operator==(const boost::function_base&,
                           boost::detail::function::useless_clear_type*)
function/function_base.hpp:589: note:
    bool boost::operator==(boost::detail::function::useless_clear_type*,
                         const boost::function_base&)

Removing the line ".def( bp::self == bp::self );" from the bindings
allows it to compile cleanly and the resulting code works correctly
from what I can tell.

Could anyone shed light on what I've done wrong and how to fix the
error?

Thanks,

Arne
_______________________________________________
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