C++ 当重载大于运算符以比较来自不同类的两个双精度值时,我得到一个操作数错误
我正在做家庭作业,以便:C++ 当重载大于运算符以比较来自不同类的两个双精度值时,我得到一个操作数错误,c++,operator-overloading,logical-operators,friend-function,C++,Operator Overloading,Logical Operators,Friend Function,我正在做家庭作业,以便: 使用一个重载的圆添加两个圆的区域+ 使用重载函数添加两个矩形的区域+ 比较圆和矩形的面积,看看它们是否相同== 使用overloaded> 到目前为止,我已经完成了前三项。我已经为大于函数重载编写了代码,但我一直收到一个错误,指出“没有运算符”>“匹配这些操作数” 我尝试将操作符函数更改为类矩形和类圆形的成员函数。我甚至尝试过将>操作符更改为其他操作符,但仍然有相同的错误 ShapeType.h选择- friend bool operator==(const c
friend bool operator==(const circleType&,
const rectangleType&);
//Overload the operator ==
friend bool operator>(const circleType&,
const rectangleType&);
//Overload the operator >
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());
}
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;
ShapesTypeImp.cpp选择-
friend bool operator==(const circleType&,
const rectangleType&);
//Overload the operator ==
friend bool operator>(const circleType&,
const rectangleType&);
//Overload the operator >
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());
}
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;
Main.cpp选择-
friend bool operator==(const circleType&,
const rectangleType&);
//Overload the operator ==
friend bool operator>(const circleType&,
const rectangleType&);
//Overload the operator >
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());
}
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:
cout>半径;
while(cin.fail())
{
cin.clear();
cin.忽略(100,“\n”);
库特半径;
}
cout>高度;
while(cin.fail())
{
cin.clear();
cin.忽略(100,“\n”);
库特高度;
}
宽度;
while(cin.fail())
{
cin.clear();
cin.忽略(100,“\n”);
库特宽度;
}
圆。设定半径(半径);
矩形。设置尺寸(高度、宽度);
我试图找出错误,
然而,这个小小的例子正在发挥作用
cpp.sh/8excr
可能是您应该向区域函数添加常量限定符我试图得到错误,
然而,这个小小的例子正在发挥作用
cpp.sh/8excr
可能您应该向面积函数添加常量限定符运算符的重载(运算符>(常量圆类型&,常量矩形类型&)
仅适用于形式为某些圆>某些矩形
的表达式。要进行比较,必须有一个运算符>(const RectangleType&,const CircleType&)
(注意参数顺序是交换的)。您的代码没有第二个运算符。同样的注释也适用于操作符==()
。重载操作符>(const CircleType&,const RectangleType&)
仅适用于形式为一些圆>一些矩形的表达式。要进行比较,必须有一个运算符>(const RectangleType&,const CircleType&)
(注意参数顺序是交换的)。您的代码没有第二个运算符。同样的评论也适用于操作符==()
。感谢您的回复。我有两个类的面积函数的常量限定符。可能是别的吗?谢谢你的回复。我有两个类的面积函数的常量限定符。可能是别的原因吗?