【问题标题】:Cannot convert 'const T*' to 'T*&&'无法将 'const T*' 转换为 'T*&&'
【发布时间】:2016-02-13 13:25:18
【问题描述】:

我在尝试将对象引用添加到指针向量时遇到错误:

template <class Tpoint, class Tmodel> Tmodel ransac<Tpoint, Tmodel>::perform_fitting(const std::vector<Tpoint>& data){
    std::vector<Tpoint*> also_inliers;
    for (const auto& pnt : data){
        if (fit_point(pnt, may_be_model) <= t){
            also_inliers.push_back(&pnt); //error here
        }
    } // ! for range
}

来自 VS.NET 2013 的错误消息:

错误 88 错误 C2664: 'void std::vector>::push_back(cv::Point_ *const &)' : 无法将参数 1 从 'const cv::Point_ *' 转换为 'cv::Point_ *&&'

【问题讨论】:

  • 您在std::vector 的声明中缺少const。应该是std::vector&lt;Tpoint const*&gt;

标签: c++ c++11


【解决方案1】:

您将pnt 捕获为const auto&amp;,然后尝试将其推入包含非常量指针的向量中。这违反了 const 正确性。

如果您不打算修改这些指针对象,请将also_inliers 更改为std::vector&lt;const Tpoint*&gt;,如果需要修改,请通过auto&amp; 捕获。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-11
    • 1970-01-01
    • 2021-05-05
    • 2011-01-18
    • 2019-02-08
    • 2013-10-20
    • 1970-01-01
    相关资源
    最近更新 更多