我在超载后增量法方面有问题。
我的前期加薪很好。
我也有前/后减量,它们都工作得很完美。
增量体和递减体应该是相似的。唯一的区别应该是++/-,但我不知道为什么我的职位增量不会像我的职位减少那样工作。
预增量
upDate upDate::operator++() {
int julian = convertDateToJulian(datePtr[0], datePtr[1], datePtr[2]);
julian++;
convertDateToGregorian(julian);
return (*this);
}员额增量
upDate upDate::operator++(int num) {
upDate temp (*this);
int julian = convertDateToJulian(datePtr[0], datePtr[1], datePtr[2]);
julian++;
convertDateToGregorian(julian);
return temp;
} 后减量
upDate upDate::operator--(int num) {
upDate temp(*this);
int julian = convertDateToJulian(datePtr[0], datePtr[1], datePtr[2]);
julian--;
convertDateToGregorian(julian);
return temp;
}这是我的主要:
upDate d5(11, 10, 2004);
++d5;
cout << d5 << endl;
cout << "Expected November 11, 2004\n" << endl;
//not working
upDate d6(11, 11, 2004);
d5++;
cout << d6 << endl;
cout << "Expected November 12, 2004\n" << endl;
upDate d11(12, 3, 1992);
d11--;
cout << d11 << endl;
cout << "Expected: December 2, 1992\n" << endl;产出如下:
//日期原为2004年11月10日
/++incr
二00四年十一月十一日
预计日期:二零零四年十一月十一日
//日期原为2004年11月11日
//incr++
2004年11月11日//产出不应该是这样
预计日期:二零零四年十一月十二日
//日期原为1992年12月2日
//12月
(一九九二年十二月一日)
预计日期:一九九二年十二月一日
发布于 2014-03-25 21:28:00
你的主语中有一个错误:
//not working
upDate d6(11, 11, 2004);
d6++; // <---- you have d5++;
cout << d6 << endl;
cout << "Expected November 12, 2004\n" << endl;发布于 2014-03-25 21:30:46
你有个打字错误。在这个代码片段中
//not working
upDate d6(11, 11, 2004);
d5++;
cout << d6 << endl;
cout << "Expected November 12, 2004\n" << endl;将post增量运算符应用于d5
d5++;但输出的d6
cout << d6 << endl;d6没有改变。
还要考虑到增量前操作符的正确声明是
upDate & upDate::operator++();也就是说,运算符应该返回lvalue.,而您的运算符则返回一个临时对象。
https://stackoverflow.com/questions/22646449
复制相似问题