iterator_distance won't compile

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

iterator_distance won't compile

Roger House
I have not been able to get this expression to compile with MSVC++

    iterator_distance(P.edges_begin(), I)

where both operands are of type Polygon_2::Edge_const_iterator.  The comment
in the code below shows the compiler error message.  The attached file
contains the entire error message with all its gory details.

It seems that two iterators of the same type which point to the same object
should be acceptable to iterator_distance, but perhaps I fail to understand
something.  

Any help will be appreciated.

Roger House
Software Developer

// idistbug.cpp  --  Problem with the CGAL iterator_distance function

#include "CGAL\Cartesian.h"
#include "CGAL\Polygon_2.h"
#include <CGAL\Polygon_set_2.h>
#include <CGAL\circulator.h>
#include <iostream>
#include <cassert>

using namespace std;

typedef CGAL::Cartesian<double>       K;
typedef K::Point_2                    Point;
typedef CGAL::Polygon_2<K>            Polygon_2;


int main()
{
    cout << "Begin test of iterator_distance"  << endl;

    Point pointsP[] = {Point(0, 0), Point(1, 0), Point(1, 1), Point(0, 1)};
    Polygon_2 P(pointsP, pointsP+4);

    Polygon_2::Edge_const_iterator I = P.edges_begin();

    ++I;

    // The call of iterator_distance in the following statement does not
compile:
    // c:\hped\builds\sr\dev33x\rlocal\debug\build\include\stlport\stl\
    // _iterator_base.h(372) : error C2678: binary '-' : no operator found
    // which takes a left-hand operand of type
    // 'const CGAL::Polygon_2_edge_iterator<Traits_,Container_>'
    // (or there is no acceptable conversion)

    cout << "  iterator_distance = " <<
iterator_distance(P.edges_begin(), I) << endl;

    cout << "Press any nonwhitespace key and Enter to terminate"  << endl;

    string c;
    cin >> c;
}

// end idistbug.cpp

--
You are currently subscribed to cgal-discuss.
To unsubscribe or access the archives, go to
https://lists-sop.inria.fr/wws/info/cgal-discuss


c:\hped\builds\sr\dev33x\rlocal\debug\build\include\stlport\stl\
    _iterator_base.h(372) : error C2678: binary '-' : no operator found
    which takes a left-hand operand of type
    'const CGAL::Polygon_2_edge_iterator<Traits_,Container_>'
    (or there is no acceptable conversion)
        with
        [
            Traits_=K,
            Container_=stlp_std::vector<CGAL::Point_2<CGAL::Cartesian<double>>>
        ]
        c:\users\roghouse\332\build\projects\cgal_eesof\include\cgal\polygon_2\polygon_2_edge_iterator.h(137): could be 'CGAL::Polygon_2_edge_iterator<Traits_,Container_> CGAL::Polygon_2_edge_iterator<Traits_,Container_>::operator -(__w64 int)'
        with
        [
            Traits_=K,
            Container_=stlp_std::vector<CGAL::Point_2<CGAL::Cartesian<double>>>
        ]
        c:\users\roghouse\332\build\projects\cgal_eesof\include\cgal\polygon_2\polygon_2_edge_iterator.h(143): or '__w64 int CGAL::Polygon_2_edge_iterator<Traits_,Container_>::operator -(const CGAL::Polygon_2_edge_iterator<Traits_,Container_> &)'
        with
        [
            Traits_=K,
            Container_=stlp_std::vector<CGAL::Point_2<CGAL::Cartesian<double>>>
        ]
        c:\users\roghouse\332\build\projects\cgal_eesof\include\cgal\functions_on_enums.h(41): or 'CGAL::Sign CGAL::operator -(CGAL::Sign)' [found using argument-dependent lookup]
        while trying to match the argument list '(const CGAL::Polygon_2_edge_iterator<Traits_,Container_>, const CGAL::Polygon_2_edge_iterator<Traits_,Container_>)'
        with
        [
            Traits_=K,
            Container_=stlp_std::vector<CGAL::Point_2<CGAL::Cartesian<double>>>
        ]
        c:\hped\builds\sr\dev33x\rlocal\debug\build\include\stlport\stl\_iterator_base.h(378) : see reference to function template instantiation '__w64 int stlp_std::__distance<_InputIterator>(const _RandomAccessIterator &,const _RandomAccessIterator &,const stlp_std::random_access_iterator_tag &)' being compiled
        with
        [
            _InputIterator=CGAL::Polygon_2_edge_iterator<K,stlp_std::vector<CGAL::Point_2<CGAL::Cartesian<double>>>>,
            _RandomAccessIterator=CGAL::Polygon_2_edge_iterator<K,stlp_std::vector<CGAL::Point_2<CGAL::Cartesian<double>>>>
        ]
        c:\users\roghouse\332\build\projects\cgal_eesof\include\cgal\circulator.h(473) : see reference to function template instantiation '__w64 int stlp_std::distance<I>(_InputIterator,_InputIterator)' being compiled
        with
        [
            I=CGAL::Polygon_2_edge_iterator<K,stlp_std::vector<CGAL::Point_2<CGAL::Cartesian<double>>>>,
            _InputIterator=CGAL::Polygon_2_edge_iterator<K,stlp_std::vector<CGAL::Point_2<CGAL::Cartesian<double>>>>
        ]
        c:\users\roghouse\332\build\projects\cgal_eesof\include\cgal\circulator.h(484) : see reference to function template instantiation '__w64 int CGAL::I_iterator_distance<IC>(const I &,const I &,CGAL::Iterator_tag)' being compiled
        with
        [
            IC=CGAL::Polygon_2_edge_iterator<K,stlp_std::vector<CGAL::Point_2<CGAL::Cartesian<double>>>>,
            I=CGAL::Polygon_2_edge_iterator<K,stlp_std::vector<CGAL::Point_2<CGAL::Cartesian<double>>>>
        ]
        c:\users\roghouse\332\build\projects\eecad\cgaltest\idistbug.cpp(26) : see reference to function template instantiation '__w64 int CGAL::iterator_distance<CGAL::Polygon_2_edge_iterator<Traits_,Container_>>(const IC &,const IC &)' being compiled
        with
        [
            Traits_=K,
            Container_=stlp_std::vector<CGAL::Point_2<CGAL::Cartesian<double>>>,
            IC=CGAL::Polygon_2_edge_iterator<K,stlp_std::vector<CGAL::Point_2<CGAL::Cartesian<double>>>>
        ]
Reply | Threaded
Open this post in threaded view
|

Re: iterator_distance won't compile

Sylvain Pion
Administrator
Roger House a écrit :

> I have not been able to get this expression to compile with MSVC++
>
>    iterator_distance(P.edges_begin(), I)
>
> where both operands are of type Polygon_2::Edge_const_iterator.  The
> comment
> in the code below shows the compiler error message.  The attached file
> contains the entire error message with all its gory details.
>
> It seems that two iterators of the same type which point to the same object
> should be acceptable to iterator_distance, but perhaps I fail to understand
> something.
> Any help will be appreciated.

Having a quick look, it seems that some "const" are missing in
<CGAL/Polygon_2/Polygon_2_edge_iterator.h>, in the operator+ and operator-
(and other) member functions of the Polygon_2_edge_iterator class.
Could you try adding them and report back?

--
Sylvain Pion
INRIA Sophia-Antipolis
Geometrica Project-Team
CGAL, http://cgal.org/
--
You are currently subscribed to cgal-discuss.
To unsubscribe or access the archives, go to
https://lists-sop.inria.fr/wws/info/cgal-discuss
Reply | Threaded
Open this post in threaded view
|

Re: iterator_distance won't compile

Roger House
Sylvain:

Thank you for your reply.  I looked at Polygon_2_edge_iterator.h, but I
don't see where
"const" might be inserted.  Below is an excerpt from that file which
would seem to be the place to make changes.  If you can make the
required changes in a reply, I will see if they fix the
iterator_distance problem.

Thank you,

Roger House


// random access iterator requirements
    Polygon_2_edge_iterator<Traits_, Container_>&
    operator+=(difference_type n) {
      first_vertex += n;
      return *this;
    }

    Polygon_2_edge_iterator<Traits_, Container_>
    operator+(difference_type n) {
      return Polygon_2_edge_iterator<Traits_, Container_>(
        container, first_vertex + n);
    }

    Polygon_2_edge_iterator<Traits_, Container_>&
    operator-=(difference_type n) {
      return (*this) -= n;
    }

    Polygon_2_edge_iterator<Traits_, Container_>
    operator-(difference_type n) {
      return Polygon_2_edge_iterator<Traits_, Container_>(
        container, first_vertex - n);
    }

    difference_type
    operator-(const Polygon_2_edge_iterator<Traits_, Container_>& a) {
      return first_vertex - a.first_vertex;
    }



Sylvain Pion wrote:

> Roger House a écrit :
>> I have not been able to get this expression to compile with MSVC++
>>
>>    iterator_distance(P.edges_begin(), I)
>>
>> where both operands are of type Polygon_2::Edge_const_iterator.  The
>> comment
>> in the code below shows the compiler error message.  The attached file
>> contains the entire error message with all its gory details.
>>
>> It seems that two iterators of the same type which point to the same
>> object
>> should be acceptable to iterator_distance, but perhaps I fail to
>> understand
>> something. Any help will be appreciated.
>
> Having a quick look, it seems that some "const" are missing in
> <CGAL/Polygon_2/Polygon_2_edge_iterator.h>, in the operator+ and
> operator-
> (and other) member functions of the Polygon_2_edge_iterator class.
> Could you try adding them and report back?
>
--
You are currently subscribed to cgal-discuss.
To unsubscribe or access the archives, go to
https://lists-sop.inria.fr/wws/info/cgal-discuss
Reply | Threaded
Open this post in threaded view
|

Re: iterator_distance won't compile

Sylvain Pion
Administrator
Roger House a écrit :
> Sylvain:
>
> Thank you for your reply.  I looked at Polygon_2_edge_iterator.h, but I
> don't see where
> "const" might be inserted.  Below is an excerpt from that file which
> would seem to be the place to make changes.  If you can make the
> required changes in a reply, I will see if they fix the
> iterator_distance problem.

Here is the patched file attached.

--
Sylvain Pion
INRIA Sophia-Antipolis
Geometrica Project-Team
CGAL, http://cgal.org/
--
You are currently subscribed to cgal-discuss.
To unsubscribe or access the archives, go to
https://lists-sop.inria.fr/wws/info/cgal-discuss

// Copyright (c) 1997  Utrecht University (The Netherlands),
// ETH Zurich (Switzerland), Freie Universitaet Berlin (Germany),
// INRIA Sophia-Antipolis (France), Martin-Luther-University Halle-Wittenberg
// (Germany), Max-Planck-Institute Saarbruecken (Germany), RISC Linz (Austria),
// and Tel-Aviv University (Israel).  All rights reserved.
//
// This file is part of CGAL (www.cgal.org); you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; version 2.1 of the License.
// See the file LICENSE.LGPL distributed with CGAL.
//
// Licensees holding a valid commercial license may use this file in
// accordance with the commercial license agreement provided with the software.
//
// This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
// WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//
// $URL: svn+ssh://scm.gforge.inria.fr/svn/cgal/trunk/Polygon/include/CGAL/Polygon_2/Polygon_2_edge_iterator.h $
// $Id: Polygon_2_edge_iterator.h 45787 2008-09-25 19:15:00Z spion $
//
//
// Author(s)     : Wieger Wesselink, Geert-Jan Giezeman <[hidden email]>

#ifndef CGAL_POLYGON_2_EDGE_ITERATOR_H
#define CGAL_POLYGON_2_EDGE_ITERATOR_H

#include <iterator>
#include <CGAL/circulator.h>

CGAL_BEGIN_NAMESPACE

template <class Traits_, class Container_> class Polygon_2;

template <class Segment_>
class Polygon_2__Segment_ptr
{
public:
    typedef Segment_ Segment;
    Polygon_2__Segment_ptr(Segment const &seg) :m_seg(seg){}
    Segment* operator->() {return &m_seg;}
private:
    Segment m_seg;
};

template <class Traits_, class Container_>
class Polygon_2_edge_iterator {
  public:
    typedef typename std::iterator_traits<typename Container_::iterator>::iterator_category iterator_category;
    typedef typename Traits_::Segment_2 Segment_2;
    typedef typename Traits_::Segment_2 value_type;
    typedef Container_ Container;
    typedef typename Container_::const_iterator const_iterator;
    typedef typename Container_::difference_type difference_type;
    typedef Segment_2*           pointer;
    typedef Segment_2&           reference;
  private:
    const Container_* container;   // needed for dereferencing the last edge
    const_iterator first_vertex;   // points to the first vertex of the edge
  public:
    Polygon_2_edge_iterator() {}
    Polygon_2_edge_iterator(const Container_* c, const_iterator f)
      : container(c), first_vertex(f) {}

    bool operator==(
      const Polygon_2_edge_iterator<Traits_, Container_>& x) const
    {
      return first_vertex == x.first_vertex;
    }
   
    bool operator!=(
      const Polygon_2_edge_iterator<Traits_, Container_>& x) const
    {
      return !(first_vertex == x.first_vertex);
    }

    Segment_2 operator*() const {
      const_iterator second_vertex = first_vertex;
      ++second_vertex;
      if (second_vertex == container->end())
        second_vertex = container->begin();
      typename Traits_::Construct_segment_2 construct_segment_2 =
            Traits_().construct_segment_2_object();
      return construct_segment_2(*first_vertex, *second_vertex);
    }
   
    Polygon_2__Segment_ptr<Segment_2> operator->() const
        {return Polygon_2__Segment_ptr<Segment_2>(operator*());}

    Polygon_2_edge_iterator<Traits_, Container_>& operator++() {
      ++first_vertex;
      return *this;
    }

    Polygon_2_edge_iterator<Traits_, Container_> operator++(int) {
      Polygon_2_edge_iterator<Traits_, Container_> tmp = *this;
      ++*this;
      return tmp;
    }

    Polygon_2_edge_iterator<Traits_, Container_>& operator--() {
      --first_vertex;
      return *this;
    }

    Polygon_2_edge_iterator<Traits_, Container_> operator--(int) {
      Polygon_2_edge_iterator<Traits_, Container_> tmp = *this;
      --*this;
      return tmp;
    }

// random access iterator requirements
    Polygon_2_edge_iterator<Traits_, Container_>&
    operator+=(difference_type n) {
      first_vertex += n;
      return *this;
    }

    Polygon_2_edge_iterator<Traits_, Container_>
    operator+(difference_type n) const {
      return Polygon_2_edge_iterator<Traits_, Container_>(
        container, first_vertex + n);
    }

    Polygon_2_edge_iterator<Traits_, Container_>&
    operator-=(difference_type n) {
      return (*this) -= n;
    }

    Polygon_2_edge_iterator<Traits_, Container_>
    operator-(difference_type n) const {
      return Polygon_2_edge_iterator<Traits_, Container_>(
        container, first_vertex - n);
    }

    difference_type
    operator-(const Polygon_2_edge_iterator<Traits_, Container_>& a) const {
      return first_vertex - a.first_vertex;
    }

    Segment_2 operator[](int n) const {
      return *Polygon_2_edge_iterator<Traits_, Container_>(
        container, first_vertex+n);
    }

    bool operator<(const Polygon_2_edge_iterator<Traits_, Container_>& a) const
    {
      return first_vertex < a.first_vertex;
    }

    bool operator>(const Polygon_2_edge_iterator<Traits_, Container_>& a) const
    {
      return first_vertex > a.first_vertex;
    }

    bool operator<=(const Polygon_2_edge_iterator<Traits_, Container_>& a) const
    {
      return first_vertex <= a.first_vertex;
    }

    bool operator>=(const Polygon_2_edge_iterator<Traits_, Container_>& a) const
    {
      return first_vertex >= a.first_vertex;
    }

};


template <class Traits_,  class Container_>
typename Container_::difference_type
distance_type(const Polygon_2_edge_iterator<Traits_,Container_>&)
{ return Container_::difference_type(); }

template <class Traits_,  class Container_>
typename Traits_::Segment_2*
value_type(const Polygon_2_edge_iterator<Traits_,Container_>&)
{ return (typename Traits_::Segment_2 *)(0); }


//-----------------------------------------------------------------------//
//                          implementation
//-----------------------------------------------------------------------//

//--------------------------------------------------------------------//
// I don't know how to implement the following function:
//
// template <class Traits_, class Container_>
// inline
// Polygon_2_edge_iterator<Traits_, Container_>
// operator+(Container_::difference_type n,
//           Polygon_2_edge_iterator<Traits_, Container_>& a)
// { return a+n; }
//--------------------------------------------------------------------//

CGAL_END_NAMESPACE

#endif
Reply | Threaded
Open this post in threaded view
|

Re: iterator_distance won't compile

Roger House
The new file not only compiles, it works!  (That is, the one case in my
test program produces the correct result - ship it!)

Thank you very much for the rapid turn-around.

Roger House

Sylvain Pion wrote:

> Roger House a écrit :
>> Sylvain:
>>
>> Thank you for your reply.  I looked at Polygon_2_edge_iterator.h, but
>> I don't see where
>> "const" might be inserted.  Below is an excerpt from that file which
>> would seem to be the place to make changes.  If you can make the
>> required changes in a reply, I will see if they fix the
>> iterator_distance problem.
>
> Here is the patched file attached.
>
--
You are currently subscribed to cgal-discuss.
To unsubscribe or access the archives, go to
https://lists-sop.inria.fr/wws/info/cgal-discuss