比较两个用户定义的类型向量

Fai*_*alM 1 c++

我正在尝试使用!=比较两个向量,但是VS2015显示了这些错误。

Error   C2672   'operator __surrogate_func': no matching overloaded function found  
Error   C2893   Failed to specialize function template 'unknown-type std::equal_to<void>::operator ()(_Ty1 &&,_Ty2 &&) const'
Run Code Online (Sandbox Code Playgroud)

码:

#include <vector>
struct Pixel 
{
    int m_nX;
    int m_nY;

    Pixel(int x, int y)
    {
        m_nX = x;
        m_nY = y;
    }
};

int main()
{
    std::vector<Pixel> vtrPixels1;
    vtrPixels1.emplace_back(1, 2);
    vtrPixels1.emplace_back(3, 4);

    std::vector<Pixel> vtrPixels2;
    vtrPixels2.emplace_back(2, 2);
    vtrPixels2.emplace_back(3, 4);

    if (vtrPixels1 != vtrPixels2)
        vtrPixels1 = vtrPixels2;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

Smi*_*ken 5

您需要重载运算符==才能上课Pixel

struct Pixel
{
    int m_nX;
    int m_nY;

    Pixel(int x, int y)
    {
        m_nX = x;
        m_nY = y;
    }

    bool operator==(const Pixel& a) const{
        return a.m_nX == m_nX && a.m_nY == m_nY;
    }
};
Run Code Online (Sandbox Code Playgroud)