No == operator found while comparing structs in C++

前端 未结 8 953
说谎
说谎 2020-12-02 09:21

Comparing two instances of the following struct, I receive an error:

struct MyStruct1 {
    MyStruct1(const MyStruct2 &_my_struct_2, const int _an_int =          


        
8条回答
  •  时光取名叫无心
    2020-12-02 10:22

    You need to explicitly define operator == for MyStruct1.

    struct MyStruct1 {
      bool operator == (const MyStruct1 &rhs) const
      { /* your logic for comparision between "*this" and "rhs" */ }
    };
    

    Now the == comparison is legal for 2 such objects.

提交回复
热议问题