Boost logo

Boost Users :

From: Martin Magnusson (martin.magnusson_at_[hidden])
Date: 2004-08-20 01:18:37


I have written a custom iterator based on boost::iterator_facade, and I'm trying
to use this with the std::partial_sort_copy algorithm.

However, when compiling with g++ I get an error message saying that there is no
matching function for a call to counting_iterator(Vertex<3>&). The way I
interpret this is that my code is trying to construct a Vertex_Iterator from a
Vertex object, but that was not my intenton.

Is there something obvious that I have missed in my definition of
Vertex_Iterator, which should be a bidirectional iterator? A Vertex_Iterator
iterates over the vertices of a mesh, and therefore it needs a pointer to the
mesh in question. Does an iterator which returns a Vertex<3>& when dereferenced
need a constructor which takes only a Vertex<3>& argument? If so, how can I
supply it with the pointer to its parent mesh?

I hope that some of this makes sense.

//////////////////////////////////////////////
// Some typedefs from the parent class 'Mesh':
//////////////////////////////////////////////
class Mesh
{
public:
   class Vertex_Iterator;
//...
private:
   friend class Vertex_Iterator;
   typedef boost::property< boost::vertex_root_t, Vertex<3> > Vertex_Property;
   typedef boost::adjacency_list
     < boost::vecS, boost::vecS, boost::undirectedS, Vertex_Property >
     Vertex_Graph;
   typedef boost::property_map<Vertex_Graph, boost::vertex_root_t>::type
     Vertex_Data_Map;
   Vertex_Data_Map its_vertex_data;
//...
};

//////////////////////////////////////////////
// Definition of my custom iterator:
//////////////////////////////////////////////
class Mesh::Vertex_Iterator:
   public boost::iterator_facade
     <Vertex_Iterator, Vertex_Graph::vertex_iterator,
      boost::bidirectional_traversal_tag, Vertex<3>& >
{
public:
   Vertex_Iterator()
   {}

   explicit Vertex_Iterator( const Mesh* const mesh,
                            Vertex_Graph::vertex_iterator p ) :
     its_parent( mesh ),
     its_vertex( p )
   {}

private:
   friend class boost::iterator_core_access;
   Vertex<3>& dereference() const
   {
     return its_parent->its_vertex_data[*its_vertex];
   }
   void increment() { ++its_vertex; }
   void decrement() { --its_vertex; }
   bool equal( Vertex_Iterator const& other) const
   {
     return this->its_vertex == other.its_vertex;
   }

   const Mesh* const its_parent;
   Vertex_Graph::vertex_iterator its_vertex;
};

//////////////////////////////////////////////
// The call with an error looks like this:
//////////////////////////////////////////////
void Mesh::subset()
{
   //...
   Compare_Normals compare( its_mean_normal );
   Vertex<3> temp_holder[size];
   Vertex_Iterator first_vertex( this, boost::vertices( its_vertices ).first );
   Vertex_Iterator last_vertex( this, boost::vertices( its_vertices ).second );
   std::partial_sort_copy( first_vertex, last_vertex,
                          temp_holder, temp_holder + size,
                          compare );
   //...
}

//////////////////////////////////////////////
// This is gcc's error message:
//////////////////////////////////////////////
Building object file Mesh.o...
/usr/include/c++/3.3.1/bits/stl_algo.h: In function `_RandomAccessIter
    std::partial_sort_copy(_InputIter, _InputIter, _RandomAccessIter,
    _RandomAccessIter, _Compare) [with _InputIter = Mesh::Vertex_Iterator,
    _RandomAccessIter = Vertex<3>*, _Compare = Mesh::Compare_Normals]':
/home/martin/source/scan-match/Mesh.cpp:532: instantiated from here
/usr/include/c++/3.3.1/bits/stl_algo.h:2661: error: no matching function for
    call to `boost::counting_iterator<size_t, boost::use_default,
    boost::use_default>::counting_iterator(Vertex<3>&)'
/usr/local/include/boost/iterator/counting_iterator.hpp:169: error: candidates
    are: boost::counting_iterator<Incrementable, CategoryOrTraversal,
    Difference>::counting_iterator(Incrementable) [with Incrementable = size_t,
    CategoryOrTraversal = boost::use_default, Difference = boost::use_default]
/usr/local/include/boost/iterator/counting_iterator.hpp:166: error:

        boost::counting_iterator<Incrementable, CategoryOrTraversal,
    Difference>::counting_iterator(const boost::counting_iterator<Incrementable,

    CategoryOrTraversal, Difference>&) [with Incrementable = size_t,
    CategoryOrTraversal = boost::use_default, Difference = boost::use_default]
/usr/local/include/boost/iterator/counting_iterator.hpp:164: error:

        boost::counting_iterator<Incrementable, CategoryOrTraversal,
    Difference>::counting_iterator() [with Incrementable = size_t,
    CategoryOrTraversal = boost::use_default, Difference = boost::use_default]
make[1]: *** [Mesh.o] Error 1
make: *** [default_target] Error 2

Thanks
/ martin


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net