c++ - 错误 : no matching member function for call to 'upper_bound' => only on macOS => Windows and Linux are fine

标签 c++ qt std

我正在编译受 this 启发的代码使用 Qt Creator。代码在 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)
             ^

我的标题包含:

#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)
    {

    }

最佳答案

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

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

关于c++ - 错误 : no matching member function for call to 'upper_bound' => only on macOS => Windows and Linux are fine,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56054791/

相关文章:

c++ - 我想优化这个短循环

c++ - 链接器错误(MEX 找不到 OpenCV 库)

c++ - 如何使用std::find查找具有struct成员值的set元素?

c++ - 在 GTK+ 2.24 中,创建具有淡出效果的临时对话框

c++ - 如何在 QT 中将 Widget 放入另一个 widget 中?

c++ - QGeoPath 与 QGeoPolygon

c++ - 在 QT 中显示 HALCON HImage

c++ - 管理退出程序的转义键

c++ - 使用 C++ 并尝试 "nest"一些代码

c++ - 有人可以解释这个 c++ 代码的区别吗?