【问题标题】:c++ error within this context and overloading operator+此上下文中的 c++ 错误和重载 operator+
【发布时间】:2015-04-09 03:31:54
【问题描述】:

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

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

8 小时 = 1 天

12 小时 = 1 天零 4 小时

16 小时 = 2 天

operator+ 函数必须调用私有成员函数才能转换为正确的天数和小时数。

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

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

编辑

我的函数(包括 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;
}

【问题讨论】:

  • 如果您有其他与原始问题不同的问题,您应该提出另一个问题。只有在您需要更正或添加与原始问题相关的内容时才应编辑您的问题,而不是第二个问题。
  • 两个 operator+s,你声明了一个并定义了另一个
  • @RodrigoGómez 我不能,因为我最近的问题被视为不好的问题,限制了我几天提问,而且限制正在上升。
  • @RocketKatz 那么我想你需要等待,这样你才能问一个新的。我的意思是,您可以随心所欲地修改和更改您的原始问题,但这不会使它成为一个好问题,也不会鼓励人们回答它。最重要的是,您的问题已经得到了答案,因此即使是针对不同的问题,也很少有人愿意再次回答。

标签: c++


【解决方案1】:

声明

NumDays operator+();

不符合定义

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

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

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

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

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

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

请参阅 Operator overloading 以获得出色的指南。

【讨论】:

    猜你喜欢
    • 2019-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-12
    • 1970-01-01
    • 1970-01-01
    • 2019-07-24
    • 1970-01-01
    相关资源
    最近更新 更多