gpt4 book ai didi

c++ - 重载后缀运算符不起作用

转载 作者:行者123 更新时间:2023-11-28 06:58:37 24 4
gpt4 key购买 nike

#include <iostream>

using namespace std;

class NumDays
{
private:
int hour;
int day;
void simplify();

public:
NumDays()
{
day = 0;
hour = 0;
}

void setData(int d, int h)
{
hour = h;
day = d;
simplify();
}

int getHour()
{
return hour;
}

int getDay()
{
return day;
}

NumDays operator++(int);
NumDays operator--(int);

};

void NumDays::simplify()
{
hour = 8*day + hour;
day = hour / 8;
hour = hour % 8;
}

NumDays NumDays::operator++(int)
{
NumDays obj1;

hour++;
simplify();
return obj1;
}

NumDays NumDays::operator--(int)
{
NumDays obj1;

hour--;
simplify();
return obj1;
}

void setFirst(NumDays &);
void setSecond(NumDays &);

void addData(NumDays &, NumDays &, NumDays &);

int main()
{
NumDays first, second, third;

setFirst(first);
setSecond(second);

addData(first, second, third);
}

void setFirst(NumDays &obj1)
{
int day, hour = 0;
cout << "Please enter the amount of days followed by hours." << endl;
cin >> day >> hour;
obj1.setData(day, hour);
}

void setSecond(NumDays &obj2)
{
int day, hour = 0;
cout << "Please enter the amount of days followed by hours again." << endl;
cin >> day >> hour;
obj2.setData(day, hour);
}

void addData(NumDays &obj1, NumDays &obj2, NumDays &obj3)
{
for (int k = 0; k < 8; k++)
{
obj3 = obj1++;
cout << " First Data: " << obj3.getDay() << " day(s), "
<< obj3.getHour() << " hour(s).";
cout << " First Data: " << obj1.getDay() << " day(s), "
<< obj1.getHour() << " hour(s).\n";
}

cout << endl;

for (int l = 0; l < 8; l++)
{
obj3 = obj2++;
cout << " Second Data: " << obj3.getDay() << " day(s), "
<< obj3.getHour() << " hour(s).";
cout << " Second Data: " << obj2.getDay() << " day(s), "
<< obj2.getHour() << " hour(s).\n";
}
}

当我运行该文件时,obj3 有 0 天和 0 小时,而 obj2 增加了。怎么会这样?当我尝试将它作为 obj3 = obj2 没有任何后缀符号时,它会复制它。所以我假设从 obj2 获取数据没有问题。但是当我包含后缀运算符时,数据变成了 0 和 0。

最佳答案

你几乎是对的。首先,我对你的类(class)做了一些改动。

class NumDays
{
private:
int hour;
int day;
void simplify();

public:
NumDays(int dy = 0, int hr = 0) : hour(hr), day(dy) { simplify(); }
void setData(int d, int h)
{
hour = h;
day = d;
simplify();
}
int getHour() const { return hour; }
int getDay() const { return day; }
friend ostream& operator<<(ostream& out, const NumDays &nd) {
return out << " First Data: " << nd.getDay() << " day(s), "
<< nd.getHour() << " hour(s).";
}
NumDays operator++(int);
};

我做的第一件事是重写构造函数以使用默认参数并使用现代风格。

我做的第二件事是将 const 添加到不(也不应该)修改对象的各种成员函数中。

我做的第三件事是添加一个 ostream 提取器作为 friend 函数。这纯粹是为了方便排查问题。

这是新版本的后增量运算符的样子:

NumDays NumDays::operator++(int)
{
NumDays obj1(*this);
hour++;
simplify();
return obj1;
}

唯一的区别是 obj1 的创建现在使用编译器生成的默认复制构造函数。这很重要,因为我们需要为后增量运算符返回未增量值的拷贝。 (您的后缀递减运算符需要类似的修复。)

我没有对您的 simplify() 例程进行任何更改。

然后我用这段代码测试了它:

int main()
{
NumDays nd(2,3);
cout << nd++ << endl; // prints 2, 3
cout << nd << endl; // prints 2, 4
return 0;
}

现在一切正常。

关于c++ - 重载后缀运算符不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22850876/

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