[C++-sig] .def(str(self)) compile problems
Simon Burton
simon at arrowtheory.com
Mon Feb 21 09:20:38 CET 2005
On Sun, 20 Feb 2005 05:24:52 -0800 (PST)
"Ralf W. Grosse-Kunstleve" <rwgk at yahoo.com> wrote:
>
> --- Simon Burton <simon at arrowtheory.com> wrote:
>
> > I'm having trouble getting the __str__ wrapper for my C++ class to compile.
> > Everything else i've tried works fine (methods/functions/operators), but when
> > I include the .def( str(self) )
> > line the compilation fails.
>
> I am not sure, but I don't think Boost.Python supports what you want. I am not
> even sure it could support automatic conversion from std::ostream to
> std::string. However, I am sure you can get what you want in simple ways. For
> example:
Thanks for those suggestions.
The tutorial has a very suggestive example:
class Rational
{ operator double() const; };
Rational pow(Rational, Rational);
Rational abs(Rational);
ostream& operator<<(ostream&,Rational);
class_<Rational>()
.def(float_(self)) // __float__
.def(pow(self, other<Rational>)) // __pow__
.def(abs(self)) // __abs__
.def(str(self)) // __str__
;
Which I modified thus:
#include <boost/python.hpp>
using namespace boost::python;
using std::ostream;
class Rational {public:};
Rational pow(Rational a, Rational b) { return a; };
Rational abs(Rational a) { return a; };
ostream& operator<<(ostream& x,Rational a) { return x << 0.0; };
BOOST_PYTHON_MODULE(rational)
{
class_<Rational>()
.def(float_(self)) // __float__
// .def(pow(self, other<Rational>)) // this line causes a syntax error
.def(abs(self)) // __abs__
.def(str(self)) // __str__
;
}
And this produces a similar compiler error:
rational.cpp: In function `void init_module_rational()':
rational.cpp:27: error: no matching function for call to `boost::python::class_<Rational, boost::python::detail::not_specified, boost::python::detail::not_specified, boost::python::detail::not_specified>::class_()'
/home/simon/local/boost_1_32_0/boost/python/class.hpp:161: error: candidates are: boost::python::class_<Rational, boost::python::detail::not_specified, boost::python::detail::not_specified, boost::python::detail::not_specified>::class_(const boost::python::class_<Rational, boost::python::detail::not_specified, boost::python::detail::not_specified, boost::python::detail::not_specified>&)
/home/simon/local/boost_1_32_0/boost/python/class.hpp:599: error: boost::python::class_<T, X1, X2, X3>::class_(const char*, const char*, boost::python::no_init_t) [with W = Rational, X1 = boost::python::detail::not_specified, X2 = boost::python::detail::not_specified, X3 = boost::python::detail::not_specified]
/home/simon/local/boost_1_32_0/boost/python/class.hpp:592: error: boost::python::class_<T, X1, X2, X3>::class_(const char*, boost::python::no_init_t) [with W = Rational, X1 = boost::python::detail::not_specified, X2 = boost::python::detail::not_specified, X3 = boost::python::detail::not_specified]
/home/simon/local/boost_1_32_0/boost/python/class.hpp:584: error: boost::python::class_<T, X1, X2, X3>::class_(const char*, const char*) [with W = Rational, X1 = boost::python::detail::not_specified, X2 = boost::python::detail::not_specified, X3 = boost::python::detail::not_specified]
Flumoxed.
Simon.
--
Simon Burton, B.Sc.
Licensed PO Box 8066
ANU Canberra 2601
Australia
Ph. 61 02 6249 6940
http://arrowtheory.com
More information about the Cplusplus-sig
mailing list