gpt4 book ai didi

c++ - 错误 C2679 : binary '>' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion)

转载 作者:行者123 更新时间:2023-11-28 04:19:55 25 4
gpt4 key购买 nike

无论出于何种原因,当我尝试比较特定仿函数中的两个 int 值时,它只会给我这个错误

MVCE

首先是我调用仿函数的部分

其次是类的主体

第三个是仿函数的主体

int main()
{
int compare;
std::vector<int> vectInt({ 1, 2, 11, 12, 34 });
std::cout << "Enter value for comparing: ";
std::cin >> compare;
int count = countGreater<int>()(vectInt, compare);
return 0;
}

class SquareTriangle
{
int cathetus1, cathetus2;
public:
SquareTriangle() {}
~SquareTriangle() {}
SquareTriangle(int first, int second)
{
this->cathetus1 = first;
this->cathetus2 = second;
}
double getArea() const
{
return (cathetus1 * cathetus2) / 2;
}
friend bool operator < (const SquareTriangle &first, const SquareTriangle &second)
{
if (first.getArea() < second.getArea())
return true;
else
return false;
}

friend bool operator > (const SquareTriangle &first, const SquareTriangle &second)
{
if (first.getArea() > second.getArea())
return true;
else
return false;
}
friend double operator << (const SquareTriangle &first, const SquareTriangle &second)
{
return first.getArea();
}
friend double operator += (const SquareTriangle &first, const SquareTriangle &second)
{
first.getArea() += second.getArea();
return first.getArea();
}
};
typedef SquareTriangle ST;

template <typename Q>
class countGreater
{
int count = 0;
public:
int operator () (const std::vector<Q> &ptr, int compare = 0)
{
if (sizeof(Q) == sizeof(ST))
{
int first, second;
std::cout << "Enter first cathetus: ";
std::cin >> first;
std::cout << "Enter second cathetus: ";
std::cin >> second;
ST valueST(first, second);
for (int i = 0; i < ptr.size(); i++)
{
if (ptr[i] > valueST)
{
count++;
}
}
}
else
{
for (int i = 0; i < ptr.size(); i++)
{
*if (ptr[i] > compare)*
{
count++;
}
}
}
std::cout << "Number of elements greater than chosen: ";
return count;
}
};

出错的行

if (ptr[i] > compare)

完整的错误信息C2679: 二进制“>”: 未找到采用“int”类型右手操作数的运算符(或没有可接受的转换)

最佳答案

如果我重新排序并修复 #includes,您的代码中至少有两个错误。首先,这不会编译:

friend double operator += (const SquareTriangle &first, const SquareTriangle &second)
{
first.getArea() += second.getArea();
return first.getArea();
}

因为您正在尝试将某些内容分配给表达式。如果您想增加first 的面积,您将不得不修改数据成员(catheti)。不确定你为什么要这样做,因为无论如何它都没有多大意义。


其次,if 行不编译:

if (ptr[i] > valueST)
{
count++;
}

因为 ptr[i] 最终是一个整数,而 valueST 是您的类的一个实例。由于您无法将 intSquareTriangle 进行比较,因此它会中断。不过,不确定您要做什么。与该地区相比,也许?

关于c++ - 错误 C2679 : binary '>' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55694273/

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