container comparison

Asked

Viewed 38 times

1

the following code works well ->

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 operator overload == pro vector, and vector types as well need to have an operator ==, because the functor std::equal_to<> is used in comparing 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:

Failure to specialize 'Unknown-type' function template'

'Operator __surrogate_func': no corresponding overloaded function found

1 answer

2


  • I didn’t think this qualifier was the cause of the error. Thanks

Browser other questions tagged

You are not signed in. Login or sign up in order to post.