当我尝试<<
通过使用另一个符号(如<<=
#include <iostream>
struct Date {
int day, month, year, hour, minute, second;
Date (int d, int m, int y, int h, int min, int s) { day = d; month = m; year = y; hour = h; minute = min; second = s; }
friend std::ostream& operator << (std::ostream&, const Date&);
friend std::ostream& operator <<= (std::ostream&, const Date&); // Carries out << but without the hour, minute, and second.
};
std::ostream& operator << (std::ostream& os, const Date& d) {
os << d.day << ' ' << d.month << ' ' << d.year << ' ' << d.hour << ' ' << d.minute << ' ' << d.second;
return os;
}
std::ostream& operator <<= (std::ostream& os, const Date& d) {
os << d.day << ' ' << d.month << ' ' << d.year;
return os;
}
int main () {
Date date(25, 12, 2021, 8, 30, 45);
std::cout << "Today is " << date << '\n'; // Works fine
std::cout << "Today is " <<= date << '\n'; // Does not work
}
如果我使用
std::cout << "Today is " <<= date;
它工作正常,那么<< '\n'
当 std::ostream& 返回时添加有什么问题<<=
?