Reputation: 71
For whatever reason while i am trying to compare two int values in a specific functor, it just gives me this error
MVCE
First goes the piece where i call functor
Second is the body of class
Third is the bofy of the functor
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;
}
};
Line that gives error
if (ptr[i] > compare)
Full error message C2679: binary '>' : no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion)
Upvotes: 0
Views: 470
Reputation: 26066
If I reorder and fix the #includes
, there are at least two mistakes in your code. first, this does not compile:
friend double operator += (const SquareTriangle &first, const SquareTriangle &second)
{
first.getArea() += second.getArea();
return first.getArea();
}
Because you are trying to assign something to an expression. If you want to increase first
's area, you will have to modify the data members (the catheti). Not sure why you are doing it, because it does not make much sense anyway.
Second, this if
line does not compile:
if (ptr[i] > valueST)
{
count++;
}
Because ptr[i]
ends up being an integer, and valueST
is an instance of your class. Since you don't have a way to compare an int
with a SquareTriangle
, it breaks. Not sure what you are trying to do, though. Comparing with the area, maybe?
Upvotes: 1