2016-10-11 77 views
1

嘗試從r-tree中刪除值時出現編譯錯誤。我還將一個原始指針與框一起存儲,這似乎是導致問題 - 如果我存儲int,字符串或shared_ptr,則不會收到錯誤。在boost R樹中存儲原始指針的問題

我沒有選擇切換到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相同的錯誤)。

回答

1

我結束了創建的原始指針的包裝:

struct wrapData { 
    public: 
    wrapData(Data *data) { _data = data; } 
    operator Data*() const { return _data; } 
    private: 
    Data *_data; 
}; 

typedef std::pair<box_t, wrapData> value_t; 
1

我得到了同樣的問題,我找到另一種方式來轉轉:

重新定義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提高1.61.0(也GCC 4.9.3,推動1.58.0)

this ticket

+0

啓發我喜歡你的解決方案比我的好,它使大量的清潔劑來訪問後臺的數據。 –