comparison of containers

1

The following code works fine - >

std::vector<int> vec = { 1, 2, 3, 4, 5 };
std::vector<int> mec = { 1, 2, 3, 4, 5 }; 

if (vec == mec)
{
    std::cout << "true" << std::endl;
}

there is an overload of the == pro pro operator, and the types in the vector also need to have a == operator, since the std::equal_to<> functor is used in the comparison of the elements, but I can not understand why the following code does not compile - >

class Stack
{
public:
    Stack(int x) : x(x)
    {

    }

    bool operator==(const Stack& e)
    {
        return this->x == e.x;
    }

private:
    int x;
};

int main()
{
    std::vector<Stack> vec = { Stack(1), Stack(2), Stack(3) };
    std::vector<Stack> mec = { Stack(1), Stack(2), Stack(3) };

    if ( vec == mec)
    {
        std::cout << "true" << std::endl;
    }

    getchar();
    return 0;
}

visual studio shows the following 2 errors:

failed to specialize template function 'unknown-type std :: equal_to :: operator () (_ Ty1 & &, _ Ty2 & &) const'

'operator __surrogate_func': no corresponding overloaded function found

    
asked by anonymous 14.04.2018 / 08:03

1 answer

2

The reserved word const was missing at the end of the == operator statement:

bool operator==(const Stack& e) const
{                             //  ^-----aqui
    ...
}

See Ideone how it works

    
14.04.2018 / 10:46