【问题标题】:error: invalid initialization of non-const reference of type 'cv::Mat&'错误:“cv::Mat&”类型的非常量引用的初始化无效
【发布时间】:2013-09-04 02:28:52
【问题描述】:

下面是我的函数原型。

void devectorize(Mat &vec,Mat mask,Mat& img);

当我尝试在我的代码中调用此函数时出现以下错误。会是什么原因?

Mat V;

for(int i=0;i<V.cols;i++)
{
  devectorize(V.col(i),mask,E_img); //Error in this line
}
error: invalid initialization of non-const reference of type 'cv::Mat&' from an rvalue of type 'cv::Mat'
utils.h:11:6: error: in passing argument 1 of 'void devectorize(cv::Mat&, cv::Mat, cv::Mat&)'

【问题讨论】:

标签: c++ opencv reference pass-by-reference argument-passing


【解决方案1】:

您不能将非常量引用绑定到临时引用。在您的情况下, devectorize 的第一个参数是非常量引用,V.col(i) 的返回值是临时的。这段代码可以工作

for (int i = 0; i < V.cols; i++)
{
    Mat tmp = V.col(i);
    devectorize(tmp, mask, E_img);
}

因此将 devectorize 的第一个参数更改为const Mat&amp;

【讨论】:

  • 你节省了我的时间。谢谢楼主,欠你一个。愉快的一天。我也感谢@user2727765 提出的问题
猜你喜欢
  • 2013-10-11
  • 1970-01-01
  • 2015-01-26
  • 1970-01-01
  • 2023-04-04
  • 2013-01-07
  • 2012-01-07
  • 2021-10-26
相关资源
最近更新 更多