Operator Overloading C++; too many parameters for << operation

You are overloading << operator as a member function, therefore, the first parameter is implicitly the calling object.

You should either overload it as friend function or as a free function. For example:

overloading as friend function.

friend ostream& operator<<(ostream& out, int x){
     out << names[x] << " " << ages[x] <<endl;
     return out;
}

However, the canonical way is to overload it as free function. You can find very good information from this post: C++ operator overloading

Leave a Comment