【问题标题】:Error C2064: term does not evaluate to a function taking 1 argument - Lambda function错误 C2064:术语不计算为采用 1 个参数的函数 - Lambda 函数
【发布时间】:2018-10-27 17:21:30
【问题描述】:

我在构建代码时遇到以下错误:

Error C2064: term does not evaluate to a function taking 1 argument

我阅读了有关此错误的其他帖子,并在我的 lambda 函数中添加了关键字 this,但我仍然遇到同样的错误。

我正在尝试使用 C++ lambda 函数 (std::remove_if) 在另一个类成员函数中使用一个类成员函数。

class LineSegments
{
public:
   LineSegments();

   float getClockwiseAngle0to180(const float& dx1, const float& dx2, const float& dy1, const float& dy2);
   void eliminateParallelLineSegments(std::vector<cv::Vec4f>& lines);
   ~LineSegments();
};

// Constructors
LineSegments::LineSegments() {}

// Destructors
LineSegments::~LineSegments() {}

float LineSegments::getClockwiseAngle0to180(const float& dx1, const float& dx2, const float& dy1, const float& dy2) {
   float dot = dx1 * dx2 + dy1 * dy2;
   float det = dx1 * dy2 - dy1 * dx2;
   float angle = -atan2(det, dot);
   angle = angle * (180 / CV_PI);
   if (angle < 0) {
      angle = angle + 360;
   }
   if (angle >= 180) {
      angle = angle - 180;
   }
   return angle;
}

void LineSegments::eliminateParallelLineSegments(std::vector<cv::Vec4f>& lines)
{
   lines.erase(remove_if(lines.begin() + 1, lines.end(), [this](const Vec4f& left_line, const Vec4f& right_line)
      {
         return (abs((getClockwiseAngle0to180(0, left_line[2] - left_line[0], 1, left_line[3] - left_line[1]) - getClockwiseAngle0to180(0, right_line[2] - right_line[0], 1, right_line[3] - right_line[1]))) < 2);
      })
      , lines.end()
   );
}

我想这是一个简单的错误,但我现在已经超过一个小时找不到它;)。这只是一个小例子。

【问题讨论】:

  • 在一个完全不相关的注释中,这些函数似乎没有任何意义是类的成员。

标签: c++ algorithm c++11 lambda remove-if


【解决方案1】:

问题在于您的 std::remove_if() 的谓词应该是一元谓词。 (一元谓词只是接受单个参数并返回布尔值的函数)。

看到这个:http://en.cppreference.com/w/cpp/algorithm/remove

std::remove_if()的参数

  1. first,last:将迭代器转发到一系列可移动元素中的初始位置和最终位置。使用的范围是 [first,last),包含 first 和 last 之间的所有元素, 包括 first 指向的元素,但不包括指向的元素 最后。

  2. pred :接受范围内的元素作为参数并返回可转换为布尔值的一元函数。返回的值 指示是否要删除元素(如果为真,则为 删除)。该函数不得修改其参数。这既可以 是函数指针或函数对象。

[this] (const Vec4f &left_line, const Vec4f &right_line) 
{
     return (abs((getClockwiseAngle0to180(0, left_line[2] - left_line[0], 1, left_line[3] - left_line[1]) - getClockwiseAngle0to180(0, right_line[2] - right_line[0], 1, right_line[3] - right_line[1]))) < 2);
 })

在这里,在您的 lambda 函数 中,您尝试传递两个参数(即 const Vec4f &amp;left_lineconst Vec4f &amp;right_line),这是不可能的。它应该是一元谓词。


“我阅读了有关此错误的其他帖子并添加了关键字 this 在我的 lambda 函数中,但我仍然遇到同样的错误。”

它与当前对象无关。因此,这里不需要使用this。您还可以在使用此类对象的向量时定义一个独立的函数。或者换句话说,您可以将getClockwiseAngle0to180() 定义为非成员函数。


可能的解决方案如下。

auto left_line = Vec4f[1]; // as per your first element of your vector(Vec4f) that you want as lhs

lines.erase(std::remove_if(lines.begin() + 1, lines.end(),
[&] (Vec4f& right_line)->bool
{
    auto temp = abs((getClockwiseAngle0to180(0, left_line[2] - left_line[0], 1, left_line[3] - left_line[1]) -
                     getClockwiseAngle0to180(0, right_line[2] - right_line[0], 1, right_line[3] - right_line[1]));

    left_line = right_line; // now your left_line  = right_line

    return temp < 2 ;
}), lines.end());

PS:尚未测试。

【讨论】:

  • 你的意思是std::remove_if作为第三个参数的函数只接受一个参数。
猜你喜欢
  • 1970-01-01
  • 2020-02-19
  • 2011-09-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多