简体   繁体   中英

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

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)

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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question Error C2679 binary '=': no operator found which takes a right-hand operand of type 'int' (or there is no acceptable conversion) C2679 binary '-=': no operator found which takes a right-hand operand of type 'T' (or there is no acceptable conversion) c++ error c2679: binary '[' : no operator found which takes a right-hand operand of type 'SalesItem' (or there is no acceptable conversion) Error 1 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'rational' (or there is no acceptable conversion) Error 2 error C2679: binary '/' : no operator found which takes a right-hand operand of type (or there is no acceptable conversion) error C2679: binary '=' : no operator found which takes a right-hand operand of type 'std::vector<_Ty> *' (or there is no acceptable conversion) error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'RatNum' (or there is no acceptable conversion) Error:C2679 binary '==': no operator found which takes a right-hand operand of type 'const std::string' (or there is no acceptable conversion Error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'const char [4]' (or there is no acceptable conversion) 22 error C2679: binary '>>' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM