gpt4 book ai didi

c++ - 在 boost R-tree 中存储原始指针的问题

转载 作者:搜寻专家 更新时间:2023-10-31 01:34:14 24 4
gpt4 key购买 nike

尝试从 r-tree 中删除值时出现编译错误。我还将原始指针与框一起存储,这似乎是导致问题的原因 - 如果我存储 int、string 或 shared_ptr,我不会出错。

我没有切换到 shared_ptr 的选项,因为所有这些都来自遗留库。还有其他解决方法吗?

我的树定义如下:

namespace bg = boost::geometry;
namespace bgi = boost::geometry::index;
namespace bgm = boost::geometry::model;

typedef boost::geometry::model::point<float, 2, bg::cs::cartesian> point_t;
typedef boost::geometry::model::box<point_t> box_t;
typedef std::pair<box_t, Data*> value_t;

boost::geometry::index::rtree<value_t, boost::geometry::index::quadratic<16>> rtree;

失败的代码如下:

while(!rtree.empty()) {
auto it = rtree.begin();
auto value = *it;
rtree.remove(value); // <-- this is where the error appears.
}

错误如下:

...../boost/geometry/index/equal_to.hpp:127:60: error: ambiguous class template instantiation for 'struct boost::geometry::index::detail::equals<NdsInstance*, void>'
&& detail::equals<T2>::apply(l.second, r.second);
^
...../boost/geometry/index/equal_to.hpp:28:8: error: candidates are: struct boost::geometry::index::detail::equals<Geometry*, Tag>
struct equals<Geometry *, Tag>
^
...../boost/geometry/index/equal_to.hpp:37:8: error: struct boost::geometry::index::detail::equals<T, void>
struct equals<T, void>
^
...../boost/geometry/index/equal_to.hpp:127:60: error: incomplete type 'boost::geometry::index::detail::equals<NdsInstance*, void>' used in nested name specifier
&& detail::equals<T2>::apply(l.second, r.second);
^

完整的代码示例可以在 Colliru 上找到.我正在使用 gcc 4.9.3 和 boost 1.62(与 boost 1.61 相同的错误)。

最佳答案

我遇到了同样的问题,我找到了另一种走动的方式:

重新定义一个 equal_to 仿函数(rtree 的第四个模板参数)

示例代码:

#include <boost/geometry.hpp>

namespace bg = boost::geometry;
namespace bgi = boost::geometry::index;
namespace bgm = boost::geometry::model;

using point = bgm::point<double, 2, bg::cs::spherical_equatorial<bg::degree>>;
using value_type = std::pair<point, int*>;

struct my_equal {
using result_type = bool;
bool operator() (value_type const& v1, value_type const& v2) const {
return bg::equals(v1.first, v2.first) && v1.second == v2.second;}
};

using rtree = bgi::rtree<value_type, bgi::quadratic<16>, bgi::indexable<value_type>, my_equal>;

int main() {
int a,b;
rtree rtree;
rtree.insert(std::make_pair(point(45,45), &a));
rtree.insert(std::make_pair(point(45,45), &b));
rtree.remove(std::make_pair(point(45,45), &b));

return 0;
}

在 gcc 6.2.1 上工作,boost 1.61.0(也在 gcc 4.9.3 和 boost 1.58.0 上)

灵感来自 this ticket

关于c++ - 在 boost R-tree 中存储原始指针的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39970131/

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