I am getting the following error when building my code:
Error C2064: term does not evaluate to a function taking 1 argument
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.
I am trying to use a class member function inside another class member function using a C++ lambda function (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()
);
}
I guess it is a simple mistake but I couldn't find it for more than one hour now ;). This is just a minimal example.
The problem is with the predicate of your std::remove_if()
which should be unary predicates. (unary predicate are just functions that take a single parameter and return a Boolean value).
See this: http://en.cppreference.com/w/cpp/algorithm/remove
Parameters of
std::remove_if()
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.
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);
})
Here, in your lambda function you are trying to pass two arguments (i.e, const Vec4f &left_line
and const Vec4f &right_line
) which is not possible. It should be an unary predicate.
"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."
It has nothing to do with current object. Therefore, no need of using this
here. You could also define an independent function as you use vector of objects of this class. Or in other-words, you can define getClockwiseAngle0to180()
as a non-member function.
A possible solution could be, something like as follows.
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: it hasn't tested.