[C++-sig] Re: problems compiling simple example in boost-python documentation/tutorial...
David Abrahams
dave at boost-consulting.com
Mon Jun 7 22:27:00 CEST 2004
Peter Vandersteegen <Peter.Vandersteegen at intec.Ugent.be> writes:
> Hello, As a boost-python beginner I've started exploring the
> tutorial. I however had trouble compiling a very basic example from
> the tutorial:
> http://www.boost.org/libs/python/doc/tutorial/doc/virtual_functions_with_default_implementations.html
> Changing some lines in the tutorial code however solved the
> compiling problem. Now my question is: Does this relate to my
> compiler: intel 7.1, msvc7.0? Or something I overlooked in my code?
> I've started with a pure virtual base class, and indeed the virtual
> function can be overriden in python and called with a function in
> c++ ... (fantastic :) ) No problems at all... I recieve however
> compiler-errors (explained after program) when I however try to
> compile the exact example from the tutorial ...(see link and
> following text)
The problem is that Boost.Python is trying to export Base's copy ctor
for wrapping return-by-value functions. THat's why your addition of
noncopyable works. Actually it seems like you've got everything
figured out below.
BTW, it would be good nettiquette to leave a blank line between the
paragraphs of your postings.
> // file wraptestpoly.cpp
> #include
> <boost/python.hpp>
> using namespace boost::python;
>
> struct Base
> {
> virtual int f(void){return 0;}
> }; //*
>
> int call_f(Base& b) { return b.f(); }
>
>
> struct BaseWrap : Base
> {
> BaseWrap(PyObject* self_)
> : self(self_) {}
> virtual int f() { return call_method<int>(self, "f"); }
> int default_f() { return Base::f(); }
> PyObject* self;
> };
>
> BOOST_PYTHON_MODULE(wraptestpoly){
>
> class_<Base, BaseWrap>("Base") //*1
> .def("f", &Base::f, &BaseWrap::default_f)
> def("call_f", call_f);
> }
> //end file
> The error I recieve is:
> D:\libraries\boost_1_31_0\boost/python/object/value_holder.hpp(134):
> error: no instance of constructor "BaseWrap::BaseWrap" matches the
> argument list argument types are: (PyObject *,
> boost::reference_wrapper<const Base>::type)
>
> If I however use
> <x-tab> </x-tab> class_<Base, BaseWrap,boost::noncopyable>("Base")
> instead of //*1 --> no compiling problems.
> Another option which works is adding a copy-constructor:
> <x-tab> </x-tab> BaseWrap(PyObject* self_, Base const& copy) : Base(copy), self(self_) {}
> in the wrapperclass.
> Now my question is: what did I do wrong with this simple example?
> Could this problem arise from an outdated tutorial?
The tutorial example isn't outdated - it never worked. We've seen
about 1 problem per month with the tutorial examples since the
tutorial came out. Joel, why don't you make test cases for each of
them and put them in the regression test suite?
--
Dave Abrahams
Boost Consulting
http://www.boost-consulting.com
More information about the Cplusplus-sig
mailing list