代码之家  ›  专栏  ›  技术社区  ›  Grillteller

错误C2064:term不计算为具有1个参数的函数-Lambda函数

  •  2
  • Grillteller  · 技术社区  · 7 年前

    生成代码时出现以下错误:

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

    我阅读了其他关于这个错误的帖子,并添加了关键字 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()
       );
    }
    

    我想这是一个简单的错误,但我现在已经一个多小时没找到了;)。这只是一个很小的例子。

    1 回复  |  直到 5 年前
        1
  •  4
  •   JeJo    7 年前

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

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

    的参数 std::remove\u if()

    1. 首先,最后 :将迭代器转发到移动可分配元素序列中的初始和最终位置。使用的范围为 [first,last],它包含first和last之间的所有元素, 包括first指向的元素,但不包括first指向的元素 最后的

    2. pred公司 :一元函数,它接受范围中的元素作为参数,并返回可转换为布尔值的值。返回的值 指示是否要删除元素(如果为true,则为 删除)。函数不应修改其参数。这两者都可以 是函数指针或函数对象。

    [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 &left_line const Vec4f &right_line )这是不可能的。它应该是一元谓词。


    “我阅读了其他关于这个错误的帖子,并添加了关键词 this 在我的lambda函数中,但我仍然得到相同的错误。"

    它与当前对象无关。因此,无需使用 在这里在使用此类对象的向量时,还可以定义一个独立函数。或者换句话说,您可以定义 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());
    

    附言 :尚未测试。