user3196144 user3196144 - 11 months ago 63
C++ Question

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

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;

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)

Error I'm getting:

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

Answer Source

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 { /*...*/ }