gpt4 book ai didi

c++ - 错误 C2064 : term does not evaluate to a function taking 1 argument - Lambda function

转载 作者:行者123 更新时间:2023-11-30 02:19:47 25 4
gpt4 key购买 nike

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

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()
);
}

我想这是一个简单的错误,但我现在一个多小时都找不到它 ;)。这只是一个最小的例子。

最佳答案

问题出在您的 std::remove_if() 的谓词上,它应该是一元谓词。 (一元谓词只是采用单个参数并返回 bool 值的函数)。

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

Parameters of std::remove_if()

  1. first, last : Forward iterators to the initial and final positions in a sequence of move-assignable elements. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.

  2. pred : Unary function that accepts an element in the range as argument, and returns a value convertible to bool. The value returned indicates whether the element is to be removed (if true, it is removed). The function shall not modify its argument. This can either be a function pointer or a function object.

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


"I read the other posts concerning this error and added the keyword this inside my lambda function but I am still getting the same error."

与当前对象无关。因此,这里不需要使用this。您还可以在使用此类对象的 vector 时定义一个独立的函数。或者换句话说,您可以将 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:没有测试过。

关于c++ - 错误 C2064 : term does not evaluate to a function taking 1 argument - Lambda function,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50391467/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com