运算符重载添加两个对象错误
Operator overloading adding two objects error
我不知道为什么这个代码应该工作,但告诉我如果我想添加两个对象该怎么办在一起请当你试图回答时,请更多地使用特定于角落的
很抱歉我英语不好,我是印度人,这是我的密码。
#include<iostream>
using namespace std;
class time
{
private:
int sec;
int mint;
int hours;
public:
int Inputsec;
int Inputmint;
int Inputhours;
time(int Inputsec, int Inputmint, int Inputhours):sec(Inputsec), mint(Inputmint), hours(Inputhours){};
time operator+(time Inputobj)
{
time blah (sec+Inputsec,mint+Inputmint,hours+Inputhours);
return blah;
}
void DisplayCurrentTime()
{
cout << "The Current Time Is"<<endl<< hours<<" hours"<<endl<<mint<<"minutes"<<endl<<sec<<"seconds"<<endl;
}
};
int main()
{
time now(11,13,3);
time after(13,31,11);
time then(now+after);
then.DisplayCurrentTime();
}
代码运行良好,但它给我的输出很糟糕。我的错误在哪里?
您的加法运算符正在使用单元化的成员Inputsec
、Inputmint
和Inputhours
变量。它应该是这样的:
time operator+(time Inputobj)
{
return time(sec+InputObj.sec, mint+InputObj.mint, hours+InputObj.hours);
}
或
time operator+(time Inputobj)
{
InputObj.sec += sec;
InputObj.mint += mint;
InputObj.hours += hours;
return InputObj;
}
或者,更好的是,实现time& operator+=(const time& rhs);
并将其用于非成员添加运算符:
time operator+(time lhs, const time& rhs)
{
return lhs += rhs;
}
您有两组成员变量表示相同的内容。您不需要这种复制。
最后一句话:在标题<ctime>
中有一种叫做std::time
的东西。拥有一个名为time
和using namespace std
的类是在自讨苦吃。如果可能的话,你应该两者都避免(避免第二种肯定是可能的)。
您应该至少按如下方式重写operator+
:
time operator+(time Inputobj)
{
time blah time(sec+InputObj.sec, mint+InputObj.mint, hours+InputObj.hours);
return blah;
}
我还认为你应该使用%
运算符来获得正确的时间结果:
time operator+(time Inputobj){
int s = (sec+InputObj.sec) % 60;
int m = (sec+InputObj.sec) / 60 + (mint+InputObj.mint) % 60;
int h = (mint+InputObj.mint) / 60 + (hours+InputObj.hours) % 24;
return time(s,m,h);
}
您的运算符重载函数正在使用未初始化的变量。初始化构造函数中的变量inputsec, inputmint, Inputhours
。
此外,试试这个:
time operator+ (time Inputobj)
{
time blah (sec+Inputobj.sec, mint+Inputobj.mint, hours+Inputobj.hours);
return blah;
}
您的错误是您的publics成员与参数构造函数具有相同的名称,这些成员是统一的。试试这个:
#include <iostream>
using namespace std;
class time
{
private:
int sec;
int mint;
int hours;
public:
time(int Inputsec, int Inputmint, int Inputhours):sec(Inputsec), mint(Inputmint), hours(Inputhours)
{
};
time operator+(time Inputobj)
{
time blah (sec+Inputobj.sec, mint+Inputobj.mint, hours+Inputobj.hours);
return blah;
}
void DisplayCurrentTime()
{
cout << "The Current Time Is"<<endl<< hours<<" hours"<<endl<<mint<<"minutes"<<endl<<sec<<"seconds"<<endl;
}
};
int main()
{
time now(11,13,3);
time after(13,31,11);
time then(now+after);
then.DisplayCurrentTime();
}
相关文章:
- 如何返回一个类的两个对象相加的结果
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 停止cmake target_link_libraries将插件中静态库的两个对象文件链接到静态库本身
- 为什么C++在将一个对象复制到另一个对象时需要对这两个对象进行低级常量限定
- int数据类型的指针指向的是什么,如果是一个类的私有数据成员,我们创建了该类的两个对象?
- 具有相同特征的两个对象是否只在内存中存储一次?无论定义它们的函数是什么,都是不同的
- 如何在Qt中连接来自不同窗口的两个对象?
- C++两个对象,其中包含指向同一数组不同部分的指针
- 为什么将两个对象分配给另一个对象后,两个对象不一样?
- C++:在另外两个对象之间共享一个对象
- 在单元测试中,如何在不使用 operator== 的情况下比较两个对象,这可能会错过新成员?
- 同一类的两个对象相互引用
- 将类对象传递给两个对象
- 如何调用运算符函数添加两个对象?
- 让一个名为A的类.传递以下两个对象有什么区别:(a)obj1和(b)obj1()
- 比较矢量迭代中的两个对象C
- 当我有两个对象时<<如何重载运算符?(有关系)
- 声明一个模板函数,将模板类友元的两个对象(仅)带到这两个专用化
- 使用指针将两个对象(每个都与一个类)相关联
- 比较类的两个对象之间的数据