gpt4 book ai didi

c++ - 比较两个用户定义的类型 vector

转载 作者:行者123 更新时间:2023-11-30 01:04:57 25 4
gpt4 key购买 nike

我正在尝试使用 != 比较两个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com