2016-11-05 291 views
2

当我使用==运算符编译器进行比较时,我有两个std :: array的大小相同并存储相同的元素类型(我写的类)抛出这个错误:...\include\xutility(2919): error C2672: 'operator __surrogate_func': no matching overloaded function foundstd :: array无法与我的类作为其元素的两个数组之间进行比较

我试着比较两个数组与向量作为他们的元素,它的工作原理但比较阵列与我写的任何类我得到的错误。

测试类:

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 
+0

为什么要退'BOOL'而不是'布尔“,为什么你的函数没有返回声明? – krzaq

+0

因为你没有比较'a0'和'a1',而是数组。 –

+0

@Eli Sadoff a1和a0是数组lol –

回答

6

如果你看看operator==std::array的定义,你会注意它是为const数组定义的。这意味着你只能以const的形式访问元素,而你的Classoperator==不会。

更改其采取隐性此为const:

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

虽然它,你可能想回到bool而不是BOOL

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

链接指向您的一个答案,与问题无关,而不是'operator =='的实际定义。这是打算? – Rakete1111

+0

@ Rakete1111不,它必须是复制粘贴错误。编辑。谢谢! – krzaq

相关问题