无论出于何种原因,当我尝试比较特定函子中的两个int值时,它只会给我这个错误
MVCE
首先去我称之为函子的部分
第二个是课程的主体
第三是函子的bofy
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'的右侧操作数(或没有可接受的转换)的运算符
答案 0 :(得分:1)
如果我重新排序并修复#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
是您的类的实例。由于您无法将int
与SquareTriangle
进行比较,因此很麻烦。不过,不确定您要做什么。也许与该地区相比?