'result_type':不是'`Traits':: Less_xy_2'

时间:2018-10-18 09:59:34

标签: compiler-errors traits cgal convex-hull

我试图使用CGAL计算一组2D点的凸包。我想定义自己的 Traits 类,以尝试遵循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;添加到类NewTraits::Less_xy_2来解决编译错误。

我的问题是:

  • 这是解决问题的正确方法吗?
  • 即使文档中没有说明我为什么还要添加此成员?

1 个答案:

答案 0 :(得分:0)

  • 是的,此修复程序是正确的。
  • 您应该出于向后兼容的原因添加result_type。实际上,NewTraits::Less_xy_2是一个二进制谓词,因此是一个函子。在采用C ++ 11标准之后,就没有必要进行此添加了,因为出于相同的目的从该修订版中引入了结构result_of(请参见 eg ,{{ 3}})。相反,在C ++ 11之前,用户应该为每个可适应的函数对象手动定义result_type(并最终为参数类型),如the cplusplus result_of reference中所述。在CGAL中,有些算法使用的结构已从C ++标准中弃用或删除(请参见the old SGI overview of function objects)。您必须定义result_type的原因是为了允许这些算法正确编译。