在这里,我试图重载关系运算符。当我将重载函数应用到类的两个对象时,它正在工作,但是当我将它应用到一个对象和浮点值时,它给了我一个错误,指出“从'double'到'distance'的转换是模棱两可的”。请帮忙。
#include <iostream>
using namespace std;
class Distance
{
int iFeet;
float fInches;
public:
Distance(const float);
Distance(const int = 0, const int = 0);
bool operator >(const Distance);
};
Distance::Distance(const float p)
{
iFeet = int(p);
fInches = (p - iFeet) * 12;
}
Distance::Distance(const int a, const int b)
{
iFeet = a;
fInches = b;
}
bool Distance::operator>(const Distance dd1)
{
if (iFeet > dd1.iFeet)
return true;
if (iFeet == dd1.iFeet && fInches > dd1.fInches)
return true;
return false;
}
int main()
{
Distance D(1, 6), D2(1, 8);
if (D > D2)
cout << "D is gtreater than D2" << endl;
else
cout << "D2 is greater than D" << endl;
if (D > 5.6)
cout << "D is greateer" << endl;
else
cout << "D is not greater" << endl;
return 0;
}