Hi,
I'm trying to wrap an iterable class, which returns a custom iterator
typedef'd as const_iterator, from its begin and end methods.
When using boost::python::(iterator|iterators|range) to create an iterator
in the exposed __iter__ function in the exposed class_ def, I get a load
of compile errors:-
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/stl_iterator_base_types.h:166:53:
error: no type named 'iterator_category' in 'class mylib::MyConstIterator'
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/stl_iterator_base_types.h:167:53:
error: no type named 'value_type' in 'class mylib::MyConstIterator'
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/stl_iterator_base_types.h:168:53:
error: no type named 'difference_type' in 'class mylib::MyConstIterator'
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/stl_iterator_base_types.h:169:53:
error: no type named 'pointer' in 'class mylib::MyConstIterator'
/usr/lib/gcc/x86_64-unknown-linux-gnu/4.7.2/../../../../include/c++/4.7.2/bits/stl_iterator_base_types.h:170:53:
error: no type named 'reference' in 'class mylib::MyConstIterator'
Here's a simple example demonstrating the problem:-
// const_iterator.cpp
#include <boost/python/class.hpp>
#include <boost/python/copy_const_reference.hpp>
#include <boost/python/iterator.hpp>
#include <boost/python/module.hpp>
// External lib declaration
//
namespace notmylib {
// forward declaration
class MyConstIterator;
class MyData
{
public:
MyData(void);
virtual ~MyData(void);
// ...
typedef MyConstIterator const_iterator;
const_iterator begin(void) const;
const_iterator end(void) const;
};
class MyConstIterator
{
public:
typedef std::pair<int, int> TRange;
// constructors
MyConstIterator(void);
MyConstIterator(const MyData);
// dtor
~MyConstIterator(void) {}
// copy constructor
MyConstIterator(const MyConstIterator& iter);
// assignment operator
MyConstIterator& operator= (const MyConstIterator& iter);
// Comparison operators
bool operator==(const MyConstIterator& iter) const;
bool operator!=(const MyConstIterator& iter) const;
// Get Data Range
TRange GetRange(void) const;
// Go backwards one step
void Rewind(void);
private:
MyData m_data;
size_t m_index;
};
}
BOOST_PYTHON_MODULE(const_iterator)
{
boost::python::class_<notmylib::MyData,
boost::noncopyable>("Data")
.def("__iter__",
boost::python::iterator<
const notmylib::MyData,
boost::python::return_value_policy<
boost::python::copy_const_reference> >() )
// ...
;
}
Although this doesn't have the member function definitions, so won't work
as a complete example from Python, it completely replicates the compile
error I get with the actual class I'm trying to wrap. Command to compile,
using GCC 4.7.2:-
g++ -c -L/usr/lib -g -O1 -fPIC -I/usr/include/python2.7
const_iterator.cpp -o const_iterator.o -lboost_python
I couldn't find any examples using const_iterator in the test directory,
or elsewhere, so can't find anything to go by. I thought to manually add
those typedef's the compilers complaining about, to a Python wrapper class
derived from MyIterator. Is there another class I can inherit from, in
boost or the STL, to do this for me?
Any alternative suggestions or recommendations would be appreciated!
Kind regards,
Alex
--
_______________________________________________
Cplusplus-sig mailing list
Cplusplus-sig@python.org
http://mail.python.org/mailman/listinfo/cplusplus-sig