compiler

Hello guys.
I was writing a simple program which I have to overload the << operator so as to display the class object using cout instead of to a use a method for that.

1
2
3
4
5
6
  void operator<<(std::ostream &os, Time &t); // prototype within a header.h
//function definition in time.cpp
  void Time::operator<<(std::ostream &s, Time &t)
{
      s << t.hours << " hours " << t.minutes << " minutes << std::endl;
} 


But the compiler keeps telling that error: 'void Time::operator<<(std::ostream&, const Time&)' must take exactly one argument|. I don't know what I was doing wrong. Please someone should point me out. Thanks
the return value for operator<< should be std::ostream& not void, it should return the stream itself (the first parameter)
1
2
3
4
5
std::ostream& operator<< ( std::ostream& os, const Time &t )
{
    // ...
    return os;
}
Last edited on
don't define the << operator as a member of the class, define it as a friend.
I have done all that you pointed out; making the return type reference to ostream object. defining the function as friend instread of class method, but I'm still having the same error message.
nvrmnd I didn't look closely to your comment and you didn't say it out. I shouldn't qualify the function with the Class qualifier and should be friend instead of class method. Thanks guys
Topic archived. No new replies allowed.