C++ 20无法满足范围的约束:: remove_if()

发布于 2025-01-31 07:31:48 字数 1106 浏览 2 评论 0原文

我有以下类:

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

我尝试从具有值&lt; 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());

但是,我不断收到编译错误:

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

即使我尝试使用remove_if Overload使用&amp; node&lt; int&gt; :: item投影。

任何建议和见识都将不胜感激。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水中月 2025-02-07 07:31:48

在添加以下=操作员过载之后,它起作用:

Node<T>& operator=(Node<T> rhs) { item_ = rhs.Item(); return *this; }

我想知道为什么原始=操作员与RHS参考引用不起作用?

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文