【发布时间】:2019-04-15 17:15:51
【问题描述】:
无论出于何种原因,当我尝试比较特定函子中的两个 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' 类型右侧操作数的运算符(或没有可接受的转换)
【问题讨论】:
-
你怎么称呼这个函子?你用什么
Q?请提供minimal reproducible example -
1.不要将相关代码作为评论发布,edit 将其发布到问题中。 2. 那仍然不是minimal reproducible example,它缺少重要信息,例如
ST的定义和完整的错误信息 -
您仍然拥有三个独立的代码块,而不是 MVCE。编写一个您认为应该自行编译的代码 sn-p。
-
@ildjarn 如果我又没有做错什么,那就是。我没有包括我使用的所有库,因为我认为它们没有发挥任何作用,但如果需要我可以编辑它们
-
在将您的代码修复为实际的 MCVE (godbolt.org/z/g56o1W) 后,我在
if (ptr[i] > valueST)而不是if (ptr[i] > compare)上收到错误
标签: c++ visual-studio