简体   繁体   English

为什么这个std :: find不能比较这些对象

[英]Why can't this std::find compare these objects

Usually when I do an std::find I'll put a predicate as the third argument, but this time I thought I'd do it differently, I don't understand why it doesn't work. 通常当我做一个std :: find时我会把一个谓词作为第三个参数,但这次我以为我会用不同的方式做,我不明白为什么它不起作用。

#include <iostream>
#include <vector>
#include <algorithm>

struct RenderJob
{
    RenderJob() {};
    int renderJob_ID;
    bool operator==(RenderJob& rhs) { return rhs.renderJob_ID == this->renderJob_ID; }
};

int main()
{
    RenderJob foo;
    RenderJob foo2;
    foo == foo2; // Works

    std::vector<RenderJob> renderJobs;

    std::find(renderJobs.begin(), renderJobs.end(), foo); // Doesn't work
}

binary "==" no operator found which takes a left-handed operand of type RenderJob (or there is no acceptable conversion) 二进制“==”没有找到哪个运算符采用RenderJob类型的左手操作数(或者没有可接受的转换)

Edit:: Well thanks for the answers. 编辑::谢谢你的答案。 Here are some examples of why it fails 以下是一些失败原因的例子

    RenderJob foo;
    RenderJob foo2;
    foo == foo2; // Works

    std::vector<RenderJob> renderJobs;

    std::vector<RenderJob>::const_iterator constit = renderJobs.begin();

    *constit == foo2;  // Doesn't work

Even simpler as an illustration: 更简单的说明:

 const RenderJob* pToRenderJob;

*pToRenderJob == foo2;  // This fails because the pointed to 
                        // object is const, and cannot call the 
                        // operator== function because the actual function
                        // definition is not const. 

If it were the other way around: 如果是相反的方式:

foo2 == *pToRenderJob; // This would fail because the 
                        // operator==(RenderJob&) the actual argument 
                        // is not const. Very subtle rules

你离开了const限定符。

bool operator==(const RenderJob& rhs) const { ... }

Looks to me like a const-correctness problem. 在我看来像一个常量问题。 Try something like this instead: 尝试这样的事情:

bool operator==(RenderJob const &rhs) const { 
    return rhs.renderJob_ID == this->renderJob_ID; 
}

Doing the comparison yourself worked because you were just passing plain objects that weren't temporaries, nor were they const qualified. 做比较你自己工作,因为你只是传递不是临时的普通对象,也不是const限定。 With std::find , the comparison function will (at least usually) receive a reference to the object in the collection (which will normally be const qualified), so it needs to be able to receive a const-qualified reference. 使用std::find ,比较函数将(至少通常)接收对集合中对象的引用(通常是const限定的),因此它需要能够接收const限定引用。

But that still doesn't make sense, in the above example, the right hand side is foo2 , which is not constant, which should go to the non- const operator== . 但是这仍然没有意义,在上面的例子中,右边是foo2 ,它不是常量,应该转到非const operator== Unless there's a general rule that const and non const can't be compared. 除非有一般规则不能比较const和非const

There is no rule in the language that const and non- const objects cannot be compared. 语言中没有规则不能比较const和非const对象。 You have to make sure that they can be compared by using proper const qualifiers. 您必须确保可以使用适当的const限定符来比较它们。

The line 这条线

*pToRenderJob == foo2;

is equivalent to 相当于

pToRenderJob->operator==(foo2);

That does not work since pToRenderJob cannot be used to call a non- const member function. 这不起作用,因为pToRenderJob不能用于调用非const成员函数。 foo2 is not the problem here. foo2不是问题所在。

If you use 如果你使用

foo2 == *pToRenderJob

it is equivalent to 它相当于

foo2.operator==(*pToRenderJob)

That is also a problem since the argument to the function is a const object while your function expects a non- const reference. 这也是一个问题,因为函数的参数是一个const对象,而你的函数需要一个非const引用。 Once again, foo2 is not the problem. 再一次, foo2不是问题。

Making the function a const member function and making the argument a const reference makes sure that all combinations of const and non- const objects on both sides of the operator can be used without any problem. 使函数成为const成员函数并使参数成为const引用可确保操作符两侧的const和非const对象的所有组合都可以毫无问题地使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何比较 std::function 对象? - How can I compare std::function objects? 为什么 std::vector 不能保存常量对象? - Why can't a std::vector hold constant objects? 为什么我的项目在命名空间std中找不到“功能”? - Why can't my project find “function” in namespace std? 为什么std :: regex_search无法找到正确的重载? - Why can't std::regex_search find the right overload? 为什么是 std::pair<string, int> 不与 std::string 比较? - Why std::pair<string, int> doesn't compare with std::string? 我如何比较将std :: vectors的内容与自定义对象进行比较 - How can I compare comparing contents of std::vectors with custom objects 为什么在构造函数中使用 std::function 时不能使用赋值 init 初始化对象,而可以使用括号 init 初始化对象? - Why can't you initialize objects with the assignment init but can with the parentheses init when taking std::function in the constructor? std :: any用于无法复制构造的对象 - std::any for objects that can't be copy constructed 我的指向std :: string对象的指针列表工作不正常,我不知道为什么 - My list of pointers to std::string objects is not working right, and I can't figure out why 为什么不能使用std :: ref将对象传递给Boost.Python模块? - Why can't std::ref be used to pass objects into Boost.Python modules?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM