访问 boost::geometry 多边形中的点数据时出错

Error accessing point data in boost::geometry polygon

我正在尝试遍历提升多边形中的点以对它们执行操作。显示我的问题的简化版本:

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>

typedef boost::geometry::model::d2::point_xy<double> point_type;
typedef boost::geometry::model::polygon<point_type> polygon;

int main()
{
    polygon polygonTest;
    boost::geometry::read_wkt("POLYGON((-2 2, 2 2, 2 -2, -2 -2, -2 2))", polygonTest);

    for (point_type point : boost::geometry::exterior_ring(polygonTest))
    {
        double xCoord = point.x;
    }


    return 0;
}

我收到以下错误:

'boost::geometry::model::d2::point_xy<double,boost::geometry::cs::cartesian>::x': function call missing argument list; use '&boost::geometry::model::d2::point_xy<double,boost::geometry::cs::cartesian>::x' to create a pointer to member

我忽略了什么来解决这个问题?

您正在使用 成员函数 x。但是你忘了调用它:

    double xCoord = point.x();

工作样本见下文

Q. What am I overlooking

您忽略了错误消息中的信息。

  • 海湾合作委员会:error: cannot resolve overloaded function ‘x’ based on conversion to type ‘double’

    它告诉你你正在将一个函数x分配给一个双...

  • 铿锵声:error: reference to non-static member function must be called; did you mean to call it with no arguments?

    它甚至还列出了您可能需要的重载

Live On Coliru

#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>

typedef boost::geometry::model::d2::point_xy<double> point_type;
typedef boost::geometry::model::polygon<point_type> polygon;

int main() {
    polygon polygonTest;
    boost::geometry::read_wkt("POLYGON((-2 2, 2 2, 2 -2, -2 -2, -2 2))", polygonTest);

    for (point_type point : boost::geometry::exterior_ring(polygonTest)) {
        double xCoord = point.x();
    }
}