'result_type': 不是 '`Traits'::Less_xy_2' 的成员

'result_type': is not a member of '`Traits'::Less_xy_2'

我试图使用 CGAL 计算一组二维点的凸包。我想定义我自己的 Traits class 试图遵循 CGAL ConvexHullTraits_2 Concept Reference。我报告我写的代码:

#include <CGAL/Exact_predicates_inexact_constructions_kernel.h>
#include <CGAL/convex_hull_2.h>
#include <CGAL/convex_hull_traits_2.h>

using namespace std;

typedef CGAL::Exact_predicates_inexact_constructions_kernel K;
typedef K::Point_2 Point_2;
typedef pair<Point_2, unsigned> Point_with_info;
typedef CGAL::Convex_hull_traits_2<K> DefaultTraits;

class NewTraits
{
public:

    typedef Point_with_info Point_2;

    class Equal_2
    {
    public:
        bool operator()(Point_2 p, Point_2 q)
        {
            return DefaultTraits::Equal_2()(p.first, q.first);
        }
    };

    class Less_xy_2
    {
    public:
        bool operator()(Point_2 p, Point_2 q)
        {
            return DefaultTraits::Less_xy_2()(p.first, q.first);
        }
    };

    class Less_yx_2
    {
    public:
        bool operator()(Point_2 p, Point_2 q)
        {
            return DefaultTraits::Less_yx_2()(p.first, q.first);
        }
    };

    class Left_turn_2
    {
    public:
        bool operator()(Point_2 p, Point_2 q, Point_2 r) const
        {
            return DefaultTraits::Left_turn_2()(p.first, q.first, r.first);
        }
    };

    class Less_signed_distance_to_line_2
    {
    public:
        bool operator()(Point_2 p, Point_2 q, Point_2 r, Point_2 s)
        {
            return DefaultTraits::Less_signed_distance_to_line_2()(p.first, q.first, r.first, s.first);
        }
    };

    class Less_rotate_ccw_2
    {
    public:
        bool operator()(Point_2 e, Point_2 p, Point_2 q)
        {
            return DefaultTraits::Less_rotate_ccw_2()(e.first, p.first, q.first);
        }
    };

    class Orientation_2
    {
    public:
        CGAL::Orientation operator()(Point_2 e, Point_2 p, Point_2 q)
        {
            return DefaultTraits::Orientation_2()(e.first, p.first, q.first);
        }
    };

    NewTraits(NewTraits &t) {};
    NewTraits() {};

    Equal_2 equal_2_object() const
    {
        return Equal_2();
    }

    Less_xy_2 less_xy_2_object() const
    {
        return Less_xy_2();
    }

    Less_yx_2 less_yx_2_object() const
    {
        return Less_yx_2();
    }

    Less_signed_distance_to_line_2 less_signed_distance_to_line_2_object()
    {
        return Less_signed_distance_to_line_2();
    }

    Less_rotate_ccw_2 less_rotate_ccw_2_object()
    {
        return Less_rotate_ccw_2();
    }

    Left_turn_2 left_turn_2_object() const
    {
        return Left_turn_2();
    }

    Orientation_2 orientation_2_object() const
    {
        return Orientation_2();
    }
};

typedef vector<Point_with_info> Set_of_points_with_info;

int main()
{
    Set_of_points_with_info points;
    Set_of_points_with_info result;

    for (int i = 0; i < 5; i++)
    {
        points.push_back(make_pair(Point_2(i, i), i));
    }

    CGAL::convex_hull_2(points.begin(), points.end(), back_inserter(result), NewTraits());

    return 0;
}

如果我编译上面的代码,我得到以下错误:

Error C2039    'result_type': is not a member of 'NewTraits::Less_xy_2'

我可以通过将 typedef K::Less_xy_2::result_type result_type; 添加到 class NewTraits::Less_xy_2.

来修复编译错误

我的问题是:

  • 是的,修复是正确的。
  • 出于向后兼容的原因,您应该添加 result_type。事实上,NewTraits::Less_xy_2 是一个二元谓词,因此是一个函子。在采用 C++11 标准之后,这个添加就没有必要了,因为出于同样的目的,从该修订版中引入了结构 result_of(参见 ,例如 , the cplusplus result_of reference). Before C++11, instead, the user was supposed to manually define result_type (and eventually the argument types) for every adaptable function object, as explained in the old SGI overview of function objects. In CGAL, there are algorithms that use structs which have been deprecated or removed from the C++ standard (see the CGAL::cpp98 namespace reference).您必须定义 result_type 的原因是为了让这些算法能够正确编译。