error: no matching member function for call to 'upper_bound' => only on macOS => Windows and Linux are fine

error: no matching member function for call to 'upper_bound' => only on macOS => Windows and Linux are fine

我正在使用 Qt Creator 编译受 this 启发的代码。代码在 Windows 10 和 openSUSE Leap 15 上编译良好,但在 macOS High Sierra 上抛出此错误:

error: no matching member function for call to 'upper_bound'
        auto end = band.upper_bound({ 0, last->y + d });
                   ~~~~~^~~~~~~~~~~
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/include/c++/v1/set:692:14: note: candidate function not viable: cannot convert initializer list argument to 'const std::__1::set<Point, std::__1::less<Point>, std::__1::allocator<Point> >::key_type' (aka 'const Point')
    iterator upper_bound(const key_type& __k)
             ^

我的 header 包含:

#include <algorithm>
#include <cmath>
#include <iostream>

struct Point {
    float x{0}, y{0};

    // Used by the `set<point>` to keep the points in the band
    // sorted by Y coordinates.
    bool operator<(Point const &other) const {
        return y < other.y;
    }

    friend std::ostream &operator<<(std::ostream &os, Point const &p) {
        return os << "(" << p.x << ", " << p.y << ")";
    }

};

我的来源包含:

#include <set>

std::pair<Point, Point> nearest_pair(std::vector<Point> points)
{
    std::sort(points.begin(), points.end(),
              [](Point const &a, Point const &b) {
        return a.x < b.x;
    }
    );

    // First and last points from `point` that are currently in the "band".
    auto first = points.cbegin();
    auto last = first + 1;

    // The two closest points we've found so far:
    auto first_point = *first;
    auto second_point = *last;

    std::set<Point> band{ *first, *last };

    float d = dist(*first, *last);

    while (++last != points.end()) {
        while (last->x - first->x > d) {
            band.erase(*first);
            ++first;
        }

        auto begin = band.lower_bound({ 0, last->y - d }); // ERROR line
        auto end = band.upper_bound({ 0, last->y + d });   // ERROR line

        assert(std::distance(begin, end) <= 6);

        for (auto p = begin; p != end; ++p) {
            if (d > dist(*p, *last)) {
                first_point = *p;
                second_point = *last;
                d = dist(first_point, second_point);
            }
        }

        band.insert(*last);
    }
    return std::make_pair(first_point, second_point);
}

我想我需要修改我的 Point 结构,但我不确定如何修改。有人可以帮忙吗?


更新

通过将这两个构造函数添加到 Point 结构解决了错误:

    Point(){

    }

    Point(float x, float y):
        x(x)
      , y(y)
    {

    }

编译器错误清楚地解释了它无法从表达式 band.lower_bound({ 0, last->y - d }) 的初始化列表中隐式构造 Point 类型的对象。该错误是有效的,因为 Point 结构没有为这些参数提供适当的用户定义的构造函数。 要解决该问题,您必须添加一个接受两个浮点参数的构造函数。

不幸的是,我没有回答为什么其他编译器不抱怨的问题。可能是使用不同选项编译的应用程序。