重载运算符类型错误
Overloaded Operator Type Error
我创建了一个 Money
class,它使用以下函数将 Money
转换为其值的百分比。我正在尝试创建一个重载运算符来完成相同的操作,但是我在下面的重载运算符中得到 Error: Expression must have integral or unscoped enum type
for scaledCents
;它们在其他方面是相同的。如何修改?提前致谢。
Money Money::percent(const Money& amount, double percentage) const {
int amountToCents = amount.getCents() + amount.getDollars() * 100;
double pScaledMoney = amountToCents * percentage;
int scaledDollars = abs((round(fabs(pScaledMoney / 100))) / 100);
int scaledCents = abs((round(fabs(pScaledMoney / 100))) % 100);
if (pScaledMoney < 0) {
scaledDollars = -scaledDollars;
scaledCents = -scaledCents;
}
return Money(scaledDollars, scaledCents);
}
运算符重载:
const Money operator %(const Money& amount, double percentage) {
int amountToCents = amount.getCents() + amount.getDollars() * 100;
double pScaledMoney = amountToCents * percentage;
int scaledDollars = abs((round(fabs(pScaledMoney / 100))) / 100);
int scaledCents = abs((round(fabs(pScaledMoney / 100))) % 100);
if (pScaledMoney < 0) {
scaledDollars = -scaledDollars;
scaledCents = -scaledCents;
}
return Money(scaledDollars, scaledCents);
}
round()
的 returned 值具有浮点类型。数值运算符 %
不能与双精度数或浮点数一起使用:
// error
(round(fabs(pScaledMoney / 100))) % 100
// fixed
((int)round(fabs(pScaledMoney / 100))) % 100
如果您担心精度丢失,最好使用 round
函数的版本 returns 整数以避免 C++: How to round a double to an int? 和
http://blog.frama-c.com/index.php?post/2013/05/02/nearbyintf1
C99 和 C++11 具有您需要的确切功能:
long int lround (double x);
其他代码风格问题(与错误无关)
您的两个函数不使用 Money 的任何私有成员,因此其中 none 应该是朋友或 class 成员。两者都可以从 class.
中定义
如果您 return 一个新对象,从函数 return const Money
没有意义。
我创建了一个 Money
class,它使用以下函数将 Money
转换为其值的百分比。我正在尝试创建一个重载运算符来完成相同的操作,但是我在下面的重载运算符中得到 Error: Expression must have integral or unscoped enum type
for scaledCents
;它们在其他方面是相同的。如何修改?提前致谢。
Money Money::percent(const Money& amount, double percentage) const {
int amountToCents = amount.getCents() + amount.getDollars() * 100;
double pScaledMoney = amountToCents * percentage;
int scaledDollars = abs((round(fabs(pScaledMoney / 100))) / 100);
int scaledCents = abs((round(fabs(pScaledMoney / 100))) % 100);
if (pScaledMoney < 0) {
scaledDollars = -scaledDollars;
scaledCents = -scaledCents;
}
return Money(scaledDollars, scaledCents);
}
运算符重载:
const Money operator %(const Money& amount, double percentage) {
int amountToCents = amount.getCents() + amount.getDollars() * 100;
double pScaledMoney = amountToCents * percentage;
int scaledDollars = abs((round(fabs(pScaledMoney / 100))) / 100);
int scaledCents = abs((round(fabs(pScaledMoney / 100))) % 100);
if (pScaledMoney < 0) {
scaledDollars = -scaledDollars;
scaledCents = -scaledCents;
}
return Money(scaledDollars, scaledCents);
}
round()
的 returned 值具有浮点类型。数值运算符 %
不能与双精度数或浮点数一起使用:
// error
(round(fabs(pScaledMoney / 100))) % 100
// fixed
((int)round(fabs(pScaledMoney / 100))) % 100
如果您担心精度丢失,最好使用 round
函数的版本 returns 整数以避免 C++: How to round a double to an int? 和
http://blog.frama-c.com/index.php?post/2013/05/02/nearbyintf1
C99 和 C++11 具有您需要的确切功能:
long int lround (double x);
其他代码风格问题(与错误无关)
您的两个函数不使用 Money 的任何私有成员,因此其中 none 应该是朋友或 class 成员。两者都可以从 class.
中定义如果您 return 一个新对象,从函数 return const Money
没有意义。