I have the following class:
template<typename T>
class Node {
private:
T item_;
public:
T Item() const {return item_;}
Node(T item) : item_(item) {}
Node<T>& operator=(T item) { item_ = item; return *this;}
Node<T>& operator=(Node<T> &rhs) { item_ = rhs.Item(); return *this;}
};
And I try to erase nodes
object from vector
which have values < 0
.
vector<Node<int>> nodes(10);
ranges::generate(nodes, [n = -5]() mutable {return n++; });
nodes.erase(ranges::remove_if(nodes, [](const Node<int>& n) { return n.Item() < 0; }).begin(), nodes.end());
However, I keep getting compilation error:
1>C:\Projects\C++\MyProject\MyProject\MyProject.cpp(8698,22): error C2672: 'operator __surrogate_func': no matching overloaded function found
1>C:\Projects\C++\MyProject\MyProject\MyProject.cpp(8698,86): error C7602: 'std::ranges::_Remove_if_fn::operator ()': the associated constraints are not satisfied
It happens even if I try to use the remove_if
overload with &Node<int>::Item
projection.
Any advice and insight is appreciated.
It works after adding the following =
operator overload:
Node<T>& operator=(Node<T> rhs) { item_ = rhs.Item(); return *this; }
I wonder why the original =
operator overload with rhs
reference doesn't work?