Note that there are some explanatory texts on larger screens.

plurals
  1. POBoost::python and iterators from base class virtual functions
    primarykey
    data
    text
    <p>So I'm trying to write a base class to handle classes that wrap std::vector, which I have working up to defining the __iter__ function. My first approach, and the one I wish to get working, is to have the begin() and end() functions in the base class. Doing this compiles fine, but when I run the code in python I get an error that looks simiar to:</p> <blockquote> <p>Boost.Python.ArgumentError: Python argument types in</p> <blockquote> <p>Container.__iter__(Container)</p> </blockquote> <p>did not match C++ signature:</p> <blockquote> <p>__iter__(boost::python::back_reference&lt; stl_iter&lt; std::vector&lt; std::shared_ptr&lt; K > > std::allocator&lt; std::shared_ptr&lt; K > > > >&amp;>)</p> </blockquote> </blockquote> <p>The following sample extension can be tested with </p> <pre class="lang-python prettyprint-override"><code>from test import * c = Container() for i in range(10): c.append(K(str(i))) for i in c: print i </code></pre> <p>Sample extension:</p> <pre class="lang-c++ prettyprint-override"><code>#include &lt;memory&gt; #include &lt;vector&gt; #include &lt;string&gt; #include &lt;boost/python.hpp&gt; template&lt;class T&gt; T* get_pointer(std::shared_ptr&lt;T&gt; const&amp; p) { return p.get(); } template&lt;class T&gt; class stl_iter { protected: typedef typename T::value_type V; typedef typename T::iterator iter; virtual T&amp; get_vector()=0; public: virtual ~stl_iter() {} virtual void append(V item) { get_vector().push_back(item); } virtual iter begin() { return get_vector().begin(); } virtual iter end() { return get_vector().end(); } }; class K { std::string val; public: K(std::string s) : val(s) {} std::string get_val() const { return val; } }; typedef std::shared_ptr&lt;K&gt; pK; typedef std::vector&lt;pK&gt; vK; class Container : public stl_iter&lt;vK&gt; { vK items; protected: vK&amp; get_vector() { return items; } public: // Works if I uncomment these //vK::iterator begin() { return get_vector().begin(); } //vK::iterator end() { return get_vector().end(); } public: virtual ~Container() {} }; typedef std::shared_ptr&lt;Container&gt; pContainer; typedef std::vector&lt;pContainer&gt; vContainer; BOOST_PYTHON_MODULE_INIT(test) { using namespace boost::python; class_&lt;K, pK&gt;("K", init&lt;std::string&gt;()) .def("__str__", &amp;K::get_val) ; class_&lt;Container, pContainer&gt;("Container") .def("append", &amp;Container::append) .def("__iter__", range(&amp;Container::begin, &amp;Container::end)) ; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload