2
votes

I have two std::array same size and store the same element type (a class that I wrote) when I compare them using == operator compiler throws this error: ...\include\xutility(2919): error C2672: 'operator __surrogate_func': no matching overloaded function found.

I tried comparing tow arrays with vectors as their elements and it worked but comparing arrays with any class I write I'm getting that error.

Test 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) {}

};

Comparison:

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

      if (a0 == a1)
        "COOL";

Error I'm getting:

...\include\xutility(2919): error C2672: 'operator __surrogate_func': no matching overloaded function found
1
Why are you returning BOOL instead of bool and why is there no return statement in your functions?krzaq
Because you're not comparing a0 and a1, but the arrays.Eli Sadoff
@Eli Sadoff a1 and a0 are arrays lolLorence Hernandez
@LorenceHernandez I know. The operator was overloaded for Class not for std::array<Class, 3>Eli Sadoff
@user3196144 then BOOL definitely isn't the same as bool. It's a typedef for int.krzaq

1 Answers

6
votes

If you look at std::array's definition of operator==, you'll notice that it's defined for const arrays. That means you can only access elements as const, which your Class's operator== doesn't do.

Change it to take implicit this as const:

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

While at it, you probably want to return bool instead of BOOL:

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