|
Boost : |
From: jfo60540_at_[hidden]
Date: 2001-07-26 10:55:35
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.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk