我在<<操作符重遇到一个问题,重载<<以便cout可以像打印其他基本数据类型一样打印类(Time,表示时间)的实例,但是在重载<<遇到一些问题,主要在于返回类型上面,因为cout<<....整个表法式最后又会返回一个cout,那么operator<<()函数的函数原型,因该可以是
1.ostream& operator<<(const ostream& os,const Time& t),//Time是类,自己编写的计算时间的类
2.ostream operator<<(const ostream& os,const Time& t),
1&2的区别只在返回类型上,但是2就是通过不了,编译器报错,报错的内容已经截图,在附件里面,还有这个operator<<()是Time类的友元函数,问题是为什么返回不是引用就不可以?
求答案?
std::ostream operator<<(std::ostream& os, const Time& t)
{
os << "Hours: " << t.hours << "Min: " << t.min << std::endl;
return os;
}
std::ostream& operator<<(std::ostream& os, const Time& t)
{
os << "Hours: " << t.hours << "Min: " << t.min << std::endl;
return os;
}