将cout与类中的两个表达式一起使用
Using cout with two expressions in a class
我正在C++中创建一个名为Time的类,该类有3个整数作为私有成员变量。我对在C++中使用类很陌生,我正在努力找出如何解决这个特殊的问题。问题是,当我尝试这样做时:
cout << "Almost midnight: " << Time(0,0,0) - Time(0,0,1) << endl;
我遇到了一个编译器错误,我认为这是因为接受3个参数的构造函数需要以不同的方式编码,因为类中的私有变量从第一个构造函数中获取值,然后试图从第二个构造函数中减去值,所以第一个值会丢失(我相信)。那么,构造函数需要如何才能保留旧值,这样我就可以在不将其存储在命名变量中的情况下进行减法运算。
这是我的代码:
#include <iostream>
#include <cctype>
#include <cstdlib>
using namespace std;
class Time
{
private:
int hours;
int minutes;
int seconds;
void normalize();
public:
Time() {hours = minutes = seconds = 0; normalize();};
Time(int x, int y, int z);
friend Time operator + (const Time& t1, const Time& t2);
friend Time operator - (const Time& t1, const Time& t2);
friend bool operator < (const Time& t1, const Time& t2);
friend istream& operator >>(istream& ins, Time& t1);
friend ostream& operator <<(ostream& out, Time& t1);
};
Time::Time(int x, int y, int z)
{
hours = x;
minutes = y;
seconds = z;
normalize();
}
void Time::normalize()
{
int s = seconds;
int m = minutes;
int h = hours;
while(s < 0)
{
s += 60;
m--;
}
while(m < 0)
{
m += 60;
h--;
}
while(h < 0)
{
h = h + 24;
}
seconds = s % 60;
minutes = (m + s/60) % 60;
hours = (h + m/60 + s/3600) % 24;
}
istream& operator >>(istream& ins, Time& t1)
{
ins >> t1.hours;
ins >> t1.minutes;
ins >> t1.seconds;
t1.normalize();
return ins;
}
ostream& operator <<(ostream& out, Time& t1)
{
if(t1.hours < 10)
out << '0' << t1.hours << ":";
else
out << t1.hours << ":";
if(t1.minutes < 10)
out << '0' << t1.minutes << ":";
else
out << t1.minutes << ":";
if(t1.seconds < 10)
out << '0' << t1.seconds;
else
out << t1.seconds;
return out;
}
int main()
{
Time t1, t2, t3, t4;
cin >> t1;
cin >> t2;
cin >> t3;
cout << "Time1: " << t1 << endl;
cout << "Time2: " << t2 << endl;
cout << "Time3: " << t3 << endl;
t4 = t1 + t2;
cout << "Time4: " << t4 << endl;
t1 = t3 - t4;
cout << "Time1: " << t1 << endl;
if (t1 < t3)
cout << "Time1 < Time3" << endl;
else
cout << "Time3 >= Time1" << endl;
Time t5 = t2 + Time(0,0,1);
if (t5 < t2)
cout << "Time5 < Time2" << endl;
else
cout << "Time5 >= Time2" << endl;
cout << "Almost midnight: " << Time(0,0,0) - Time(0,0,1) << endl;
return 0;
}
Time operator + (const Time& t1, const Time& t2)
{
Time temp;
temp.hours = t1.hours + t2.hours;
temp.minutes = t1.minutes + t2.minutes;
temp.seconds = t1.seconds + t2.seconds;
return temp;
}
Time operator - (const Time& t1, const Time& t2)
{
Time temp;
temp.hours = t1.hours - t2.hours;
temp.minutes = t1.minutes - t2.minutes;
temp.seconds = t1.seconds - t2.seconds;
temp.normalize();
return temp;
}
bool operator < (const Time& t1, const Time& t2)
{
if(t1.hours < t2.hours && t1.minutes < t2.minutes && t1.seconds < t2.seconds)
return true;
else
return false;
}
提前谢谢。
Time(0,0,0) - Time(0,0,1)
给出一个临时对象(右值)
对过载的<<
操作员使用const Time&
friend ostream& operator <<(ostream& out, const Time& t1);
// ~~~~ Use const
相关文章:
- 将fold表达式与std::一起用于两个元组
- 查找中间两个数字的正则表达式的匹配项
- 为什么在template函数广播中把两个extensor表达式加在一起不正确
- 使用 boost::regex (c++) 比较两个正则表达式
- 正则表达式 获取两个换行符之间的文本
- 我可以写出小于 -0.5 两个 ulps 的常量表达式双精度吗?
- 表达式未评估为常数两个级别的constexpr函数(编译器错误?)
- 是否可以找到两个模板参数之间表达式的类型
- 两个指针之间的差异是合法的 c++17 常量表达式
- 无限数组C++在一个表达式中使用两个新值调整数组大小
- 逗号(,)在条件部分中两个表达式之间的for循环中的工作方式
- 计算具有两个以上操作数的后缀表达式时出现问题
- 将cout与类中的两个表达式一起使用
- 正则表达式,获取两个关键字之间的整个字符串
- 这两个表达式相等吗?
- 是否有可能在某个表达式中定义两个单词,而不仅仅是一个单词
- 错误:当通过P2P通道连接两个NodeContainers时,期望主表达式在ns3中的const之前
- 这两个比较表达式相同吗?
- 在or条件语句中是否存在强制对两个表达式求值的模式或技巧?
- c++标准算法,用最低优先级的运算符分隔一个表达式为两个表达式