简体   繁体   English

为什么 std::set 可以与我的自由函数 operator< 一起使用,但不能与我的类成员函数 operator< 一起使用?

[英]Why does std::set work with my free function operator<, but not my class member function operator<?

If I use the free function operator < , my program works.如果我使用自由函数operator < ,我的程序就可以工作。 If I use the implementation inside the class, I get a compiler error.如果我在类中使用实现,则会出现编译器错误。 Why doesn't implementing this as a class member function work?为什么不将其作为类成员函数来实现?

Part of the error I get:我得到的部分错误:

usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_function.h:386:20: error: invalid operands to binary expression ('const my' and 'const my')
      { return __x < __y; }
               ~~~ ^ ~~~

The code :编码 :

class my{

    public:
        int a;
        double b;
        my(int p){
            a=p;
            b=0;
        }
        bool operator> (const my other){
            return this->a > other.a;
        }
        bool operator < (const my other) {
            return this->a < other.a;
        }
};

//  bool operator < ( const my othe2,const my other) {
//             return othe2.a < other.a;
//         }

int main(){

    set<my> s={10,5};
    s.emplace(8);
    s.emplace(-8);

    for(auto t:s){
        cout<<t.a<<",";
    }

}

They should be overloaded with const directive.它们应该用const指令重载。 Otherwise, the non const operators can't be applied to const my whatever aka std::set<my>::key_type whatever inside std::set .否则,非 const 运算符不能应用于const my whatever aka std::set<my>::key_type whatever里面的任何内容std::set Add添加

bool operator> (const my& other) const {
    return this->a > other.a;
}
bool operator < (const my& other) const {
    return this->a < other.a;
}

Additionally other should be passed by reference const my& other .另外other应该通过引用传递const my& other

暂无
暂无

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

相关问题 我们可以将'operator&lt;'定义为 class 的成员 function 以使用 std::less 吗? - can we define 'operator<' as member function of a class to work with std::less? std :: ostream&运算符&lt;&lt;使用我类中的函数 - std::ostream & operator << using a function within my class 我的 class 成员 function 中的“操作员&gt;&gt;”不匹配,使用 set 和 get 读取文件输入 - No match for 'operator>>' in my class member function, using set and get to read file input 重载operator - ()作为自由函数而不是成员函数的意义? - Significance of overloading operator - () as free function & not a member function? 哪个优先,免费的 function 运算符==() 或成员 function 运算符==()? - Which takes precedence, the free function operator==() or the member function operator==()? 为什么|| 操作员在我的情况下工作 - Why does || operator work in my situation 为什么输出带转换运算符的类不适用于std :: string? - Why does outputting a class with a conversion operator not work for std::string? 模板转换运算符如何指向成员函数语法的指针 - How does template converting operator to pointer to member function syntax work 重载算子的scope是什么? 它是否影响作为 class 成员的集合的插入 function? - what is the scope of the overloaded operator? Does it affect the insert function for a set that is a class member? 为什么operator()在C ++ 17中为std :: function改变? - Why does operator() change for std::function in C++17?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM