我正在尝试使用!=比较两个 vector ,但是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'

码:
#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;
}

最佳答案

您需要重载==类的操作符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;
    }
};

关于c++ - 比较两个用户定义的类型 vector ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49151033/

10-11 23:14
查看更多