I'm overloading the insertion operator (<<) inside a struct using the following syntax:
struct Address{
string street;
string cross;
int suite;
friend ostream &operator <<(ostream &oss, const Address &other){
oss<<"street: "<<other.street<<"cross: "<<other.cross<<"suite: "<<other.suite;
return oss;
}
};
I see that only if I declare the function as a friend of struct 'Address' does my code compile. As per my understanding a friend function is useful when there's a need to access the private members of a class. But, since in a struct all the members are public, there shouldn't be a need to declare the '<<' operator as a friend.
Could anybody please clarify the need of declaring '<<' operator here as a friend of the struct 'Address'?