std::array 无法比较以我的 class 作为元素的两个数组

std::array Can't compare between two arrays with my class as its element

我有两个 std::array 大小相同并存储相同的元素类型(我写的 class)当我使用 == 运算符比较它们时编译器抛出此错误:...\include\xutility(2919): error C2672: 'operator __surrogate_func': no matching overloaded function found.

我尝试将两个数组与向量作为它们的元素进行比较并且它有效但是将数组与我写的任何class进行比较我收到了那个错误。

测试class:

class Class {

    int i;

public:
    Class() {}
    Class(const Class& other) {}
    Class(Class&& other) {}
    ~Class() {}

    Class operator= (const Class& other) {}
    Class operator= (Class&& other) {}

    BOOL operator== (const Class& other) {}

};

比较:

   std::array<Class, 3> a0 = {};
   std::array<Class, 3> a1 = {};

      if (a0 == a1)
        "COOL";

我遇到的错误:

...\include\xutility(2919): error C2672: 'operator __surrogate_func': no matching overloaded function found

如果您查看 std::arrayoperator== 的定义,您会注意到它是为 const 数组定义的。这意味着您只能将元素作为 const 访问,而您的 Classoperator== 不会。

将其更改为将隐含的 this 作为 const:

BOOL operator== (const Class& other) const { /*...*/ }
                                     ^^^^^

同时,您可能想要 return bool 而不是 BOOL:

bool operator== (const Class& other) const { /*...*/ }