簡體   English   中英

在boost :: geometry :: for_each_point中使用lambda

[英]Use lambda in the boost::geometry::for_each_point

我想將boost::model::polygon轉換為boost::model::multi_point ,這是我的實現:

namespace bg = boost::geometry;
typedef bg::model::point<double, 3, bg::cs::cartesian> point3d;

bg::model::multi_point<point3d> result;
std::function<void(point3d)> appendPoint = [result](point3d point){
    bg::append(result, point);
};

bg::for_each_point(polygon, appendPoint);

但是這段代碼給了我一個錯誤:

error: passing ‘boost::remove_reference<const boost::geometry::model::multi_point<boost::geometry::model::point<double, 3ul, boost::geometry::cs::cartesian> > >::type {aka const boost::geometry::model::multi_point<boost::geometry::model::point<double, 3ul, boost::geometry::cs::cartesian> >}’ as ‘this’ argument of ‘void std::vector<_Tp, _Alloc>::push_back(const value_type&) [with _Tp = boost::geometry::model::point<double, 3ul, boost::geometry::cs::cartesian>; _Alloc = std::allocator<boost::geometry::model::point<double, 3ul, boost::geometry::cs::cartesian> >; std::vector<_Tp, _Alloc>::value_type = boost::geometry::model::point<double, 3ul, boost::geometry::cs::cartesian>]’ discards qualifiers [-fpermissive]

如果我理解正確,這種錯誤表明const正確性存在問題。 但我真的不知道,這段關於const代碼有什么問題。 任何人都可以解釋我,我的錯誤在哪里,並且很熱門來解決它?

問題是bg::append的第一個參數應該是對result引用, result是通過lambda中的值捕獲的。 將捕獲更改為

[&result](point3d point)

對於按值捕獲,它會報告有關const正確性的錯誤,因為您將臨時對象作為期望非const引用的參數傳遞。 如果是常量,則不會報告錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM