gpt4 book ai didi

c++ - 当重载大于运算符以比较来自不同类的两个 double 值时,出现操作数错误

转载 作者:太空宇宙 更新时间:2023-11-04 12:31:53 24 4
gpt4 key购买 nike

我正在做家庭作业:

  1. 使用重载 + 添加 2 个圆的面积

  2. 使用重载 + 添加 2 个矩形的面积

  3. 使用重载比较圆形和矩形的面积是否相同 ==

  4. 使用重载比较圆形和矩形的面积,看哪个更大 >

到目前为止,我已经完成了前三个。我已经为大于函数重载编写了代码,但我一直收到错误消息——“没有运算符”>“匹配这些操作数”

我已经尝试将运算符函数更改为类 rectangle 和类 circle 的成员函数。我什至尝试将 > 运算符更改为其他运算符,但我仍然遇到相同的错误。

ShapesType.h选择-

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

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

ShapesTypeImp.cpp 选择-

    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选择-

                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

最佳答案

我试图得到错误,然而这个小例子是有效的cpp.sh/8excr

也许你应该给 area 函数添​​加 const 限定符

关于c++ - 当重载大于运算符以比较来自不同类的两个 double 值时,出现操作数错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58359990/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com