views:

363

answers:

1

I am trying to overload operators of a C++ class using Boost.Python.

According to this, I am doing it the right way... but I have a bunch of compiler errors.

Here is a simple example I made trying to pinpoint the problem:

#include "boost/python.hpp"

using namespace boost::python;

class number
{
public:
    number(int i) : m_Number(i) { }
    number operator+(int i) { return number(m_Number + i); }
private:
    int m_Number;
};

BOOST_PYTHON_MODULE(test)
{
    class_<number>("number", init<int>())
     .def(self() + int());
}

Here are the compiler errors:

Error   1 error C2064: term does not evaluate to a function taking 0 arguments c:\users\kevin\documents\visual studio 2008\projects\boostpythontest\boostpythontest\test.cpp 16 BoostPythonTest
Error   2 error C2780: 'boost::python::class_<W> &boost::python::class_<W>::def(const char *,Fn,const A1 &,const A2 &,const A3 &)' : expects 5 arguments - 1 provided c:\users\kevin\documents\visual studio 2008\projects\boostpythontest\boostpythontest\test.cpp 16 BoostPythonTest
Error   3 error C2780: 'boost::python::class_<W> &boost::python::class_<W>::def(const char *,Fn,const A1 &,const A2 &)' : expects 4 arguments - 1 provided c:\users\kevin\documents\visual studio 2008\projects\boostpythontest\boostpythontest\test.cpp 16 BoostPythonTest
Error   4 error C2780: 'boost::python::class_<W> &boost::python::class_<W>::def(const char *,A1,const A2 &)' : expects 3 arguments - 1 provided c:\users\kevin\documents\visual studio 2008\projects\boostpythontest\boostpythontest\test.cpp 16 BoostPythonTest
Error   5 error C2780: 'boost::python::class_<W> &boost::python::class_<W>::def(const char *,F)' : expects 2 arguments - 1 provided c:\users\kevin\documents\visual studio 2008\projects\boostpythontest\boostpythontest\test.cpp 16 BoostPythonTest

Am I missing something here ?

Thanks

+1  A: 

I've not used boost.python, but your errors look like there are some incompatible arguments when some template magic tries to bind something to something else.

So I looked at the link you provided, and found one major difference:

class_<X>("X")
    .def(self + int())

vs yours

class_<number>("number", init<int>())
    .def(self() + int());

I guess, self and self() could do that.

gimpf
You are totally right... oops.Thanks !
Kevin