简体   繁体   English

错误:没有匹配的成员函数可调用'upper_bound'=>仅在macOS上=> Windows和Linux都可以

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

I'm compiling a code inspired by this with Qt Creator. 我编译启发代码与Qt Creator的。 Code compiles fine on Windows 10 and openSUSE Leap 15 but throws this error on macOS High Sierra: 在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)
             ^

My header contains: 我的标题包含:

#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 << ")";
    }

};

My source contains: 我的资料包含:

#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);
}

I guess I need to modify my Point structure, but I'm not sure how. 我想我需要修改Point结构,但不确定如何。 Can anybody help? 有人可以帮忙吗?


Update 更新

Error got resolved by adding these two constructors to Point structure: 通过将以下两个构造函数添加到Point结构来解决错误:

    Point(){

    }

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

    }

The compiler error clearly explains that it failed to implicitly construct an object of type Point out of initializer list in expression: band.lower_bound({ 0, last->y - d }) . 编译器错误清楚地说明了它无法从表达式的初始化列表中隐式构造类型为Point的对象: band.lower_bound({ 0, last->y - d }) The error is valid, as Point structure doesn't have an appropriate user defined constructor for these arguments. 该错误是有效的,因为Point结构没有适合这些参数的用户定义的构造函数。 To solve the problem you must add a constructor that accepts two floating point arguments. 要解决该问题,您必须添加一个接受两个浮点参数的构造函数。

Unfortunately I don't answer the question on why other compilers do not complain. 不幸的是,我没有回答为什么其他编译器不会抱怨的问题。 Probably the application compiled with different options. 可能是应用程序使用不同的选项进行了编译。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM