此上下文中的 c++ 错误和重载 operator+

标签 c++

我正在处理这项作业,但在编码时遇到了问题。函数 operator+ 是上下文中的错误。我的全部或部分功能是否也可以正确用于此作业?如果不对,请指出需要更正的地方

程序创建了一个 NumDays 类,使用两个 int 成员变量来表示一个人工作了多少天和多少小时(一天总共只有 8 小时)。该程序需要使用 operator+ 以及递增 (++) 和递减 (--) 的后缀和前缀运算符。例如:

8 小时= 1 天

12 小时= 1 天零四小时

16 小时= 2 天

operator+ 函数必须调用私有(private)成员函数来转换为正确的天数和小时数。

应调用私有(private)成员函数以转换为正确的天数和小时数。

如果天数和小时数都已为 0,则递减运算符不应对该对象执行任何操作。如果对象发生更改,应调用私有(private)成员函数以转换为正确的天数和小时数。

编辑

我的函数(包括 operator+)正在运行,但是当我同时添加 onetwo 对象时,它没有返回正确的数字。我得到 0 天 2 小时。我应该在 operator+ 中返回什么?

编辑 2

终于,operator+ 开始工作了。将第一个 a.getDays() 替换为 this->day_hour + a.day_hour 有效。

到目前为止,这是我的程序:

#include <iostream>

using namespace std;

class NumDays
{
private:
    int day_hour;
public:
    NumDays(int, int);
    NumDays(int);
    void setTime(int, int);
    int getDays() const;
    int getHours() const;
    NumDays operator+(const NumDays&);
    NumDays operator++();
    NumDays operator++(int);
    NumDays operator--();
    NumDays operator--(int);
};
NumDays::NumDays(int days, int hours)
{
setTime(days, hours);
}
NumDays::NumDays(int hours)
{
day_hour=hours;
}
void NumDays::setTime(int days, int hours)
{
day_hour=8 *days+hours;
}
int NumDays::getDays() const
{
return day_hour/8;
}
int NumDays::getHours() const
{
return day_hour%8;
}
NumDays NumDays::operator+(const NumDays& a)
{
return NumDays(a.getDays()+ a.getHours());
}
NumDays NumDays::operator++()
{
day_hour++;
return *this;
}
NumDays NumDays::operator++(int)
{
NumDays time=*this;
day_hour++;
return time;
}
NumDays NumDays::operator--()
{
day_hour--;
return *this;
}
NumDays NumDays::operator--(int)
{
NumDays time=*this;
day_hour--;
return time;
}

int main()
{
NumDays one(25);
NumDays two(16);
NumDays three(0);
cout<<endl<<one.getDays()<<" days and "<<one.getHours()<<" hours."<<endl;
cout<<two.getDays()<<" days and "<<two.getHours()<<" hours."<<endl;
three=one+two;
cout<<three.getDays()<<" days and "<<three.getHours()<<" hours."<<endl;
cout<<endl;
for(int i=0; i<3; i++)
{
    one=++two;
    cout<<one.getDays()<<" days and "<<one.getHours()<<" hours."<<endl;
}
cout<<endl;
for(int i=0; i<3; i++)
{
    one=two++;
    cout<<one.getDays()<<" days and "<<one.getHours()<<" hours."<<endl;
}
cout<<endl;
for(int i=0; i<3; i++)
{
two=--one;
cout<<two.getDays()<<" days and "<<two.getHours()<<" hours."<<endl;
}
cout<<endl;
for(int i=0; i<3; i++)
{
two=one--;
cout<<two.getDays()<<" days and "<<two.getHours()<<" hours."<<endl;
}
return 0;
}

最佳答案

声明

NumDays operator+();

不符合定义

NumDays NumDays::operator+(NumDays a, NumDays b){...}

如果你想让operator+成为一个成员函数,它必须声明为

NumDays operator+(const NumDays& rhs); // you invoke it on current instance

不过,最好不要将二元运算符声明为成员函数,而是声明为friend,例如:

friend NumDays operator+(const NumDays& lhs, const NumDays& rhs);

声明它们的方式意味着您正试图在当前实例上调用它们(在这种情况下,您只需要一个参数,而不是两个)。

参见 Operator overloading以获得出色的指导。

关于此上下文中的 c++ 错误和重载 operator+,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29529260/

相关文章:

c++ - 类内外无作用域的枚举重定义

c++ - Oracle Pro*C for insert with a sub select query causing ORA-01403 : no data found

c++ - 调用 get 后将 shared_ptr 分配给另一个

c++ - 四元数到达万向节锁

c++ - 字符串文字的隐式转换

c++ - 使用 if goto 循环的正确方法

c++ - 如何通过引用从 MQL4 向 C++ DLL 传递参数

c++ - 绘制到屏幕的 DC C++ 后强制屏幕重绘

C++ 程序在使用 if 条件检查指针是否为 NULL 时崩溃

c++ - 代码块的 Boost 库设置