gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 04:18:23 29 4
gpt4 key购买 nike

我正在编译受 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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com