При перегрузке больше, чем оператор для сравнения двух двойных значений из разных классов, я получаю ошибку операнда - PullRequest
1 голос
/ 13 октября 2019

Я работаю над домашним заданием:

  1. Добавьте область из 2 кругов, используя перегруженный +

  2. Добавьте область из2 прямоугольника с использованием перегруженного +

  3. Сравните площадь круга и прямоугольника, чтобы увидеть, совпадают ли они с использованием перегруженного ==

  4. Сравнитьплощадь круга и прямоугольника, чтобы увидеть, что больше, используя перегруженный>

До сих пор я завершил первые три. Я написал код для перегрузки больше, чем функция, но я все время получаю сообщение об ошибке - "no operator"> "соответствует этим операндам"

Я попытался изменить функцию оператора, чтобы она была функцией-членом прямоугольника классаи класс кружок. Я даже пытался изменить оператор> на другие операторы, но у меня все еще та же ошибка.

ShapesType.h selection-

    friend bool operator==(const circleType&,
    const rectangleType&);
//Overload the operator ==

    friend bool operator>(const circleType&,
    const rectangleType&);
//Overload the operator >

ShapesTypeImp.cpp selection-

    bool operator== (const circleType& Circle,
    const rectangleType& rectangle)
    {
          return Circle.area() == rectangle.area();
    }

    bool operator> (const circleType& Circle,
    const rectangleType& rectangle)
    {
          return (Circle.area() > rectangle.area());
    }

Main.cpp selection-

                case 3:
            cout << "Enter the radius of the circle: ";
            cin >> radius;

            while (cin.fail())
            {
                cin.clear();
                cin.ignore(100, '\n');
                cout << endl << "Not a valid input, enter a number: ";
                cin >> radius;
            }

            cout << "Enter the height of the rectangle: ";
            cin >> height;

            while (cin.fail())
            {
                cin.clear();
                cin.ignore(100,'\n');
                cout << endl << "Not a valid input, enter a number: ";
                cin >> height;
            }

            cout << "Enter the width of the rectangle: ";
            cin >> width;

            while (cin.fail())
            {
                cin.clear();
                cin.ignore(100,'\n');
                cout << endl << "Not a valid input, enter a number: ";
                cin >> width;
            }

            circle.setRadius(radius);
            rectangle.setDimensions(height, width);
            cout << "The area of the circle is: " << circle.area() << endl;
            cout << "The area of the rectangle is: " << rectangle.area() << endl;
            if (circle == rectangle)
            {
                cout << "The area of the circle is equal to the area of the rectangle.";
            }
            else
                cout << "The area of the circle is not equal to the area of the rectangle.";

            cout << endl << endl;
            break;

        case 4:
            cout << "Enter the radius of the circle: ";
            cin >> radius;

            while (cin.fail())
            {
                cin.clear();
                cin.ignore(100, '\n');
                cout << endl << "Not a valid input, enter a number: ";
                cin >> radius;
            }

            cout << "Enter the height of the rectangle: ";
            cin >> height;

            while (cin.fail())
            {
                cin.clear();
                cin.ignore(100, '\n');
                cout << endl << "Not a valid input, enter a number: ";
                cin >> height;
            }

            cout << "Enter the width of the rectangle: ";
            cin >> width;

            while (cin.fail())
            {
                cin.clear();
                cin.ignore(100, '\n');
                cout << endl << "Not a valid input, enter a number: ";
                cin >> width;
            }

            circle.setRadius(radius);
            rectangle.setDimensions(height, width);
            cout << "The area of the circle is: " << circle.area() << endl;
            cout << "The area of the rectangle is: " << rectangle.area() << endl;
            if (circle > rectangle)
            {
                cout << "The area of the circle is greater than the area of the rectangle.";
            }
            else
                cout << "The area of the rectangle is greater than the circle.";

            cout << endl << endl;
            break;

Я включил случай 3, потому что он работает, как и ожидалось. Случай 4, использующий практически идентичный код, выдает ошибку операнда. Кажется, что он пытается использовать std> вместо перегруженного>. Я не уверен, как это исправить, и любая помощь или понимание будут с благодарностью.

Прикрепленное изображение ошибки:

Image of the error when hovering over the operator

1 Ответ

0 голосов
/ 13 октября 2019

Я попытался получить ошибку, однако этот крошечный пример работает. Cpp.sh/8excr

возможно, вам следует добавить квалификатор const в функцию area

...