First of all the overloaded operator<< should be a friend of class Name_pairs.
After that, simply iterate over the vectors and output their elements to os.
1 2 3 4 5 6 7 8
for (std::vector<std::string>::const_iterator ci = n.names.begin(); ci != n.names.end(); ++ci)
os << *ci << ' ';
// or if your compiler supports C++11 range-based for() loops
// (Visual Studio 2013, GNU GCC 4.6+)
for (const std::string &s: n.names)
os << s << ' ';
@ AeonFlux1212: while a printOn() function is a good approach too, your operator<< snippets leave me surprised, what on Earth are you doing?
@Catfish666 It's an exercise from Bjarne Stroustrup's text book, chapter 9.
I think writing a function is easier, but the exercise calls for overloading <<, which is weird, cause the chapter only shows you how to overload << for use with built in class members that are integers.
I think writing a function is easier, but the exercise calls for overloading <<, which is weird, cause the chapter only shows you how to overload << for use with built in class members that are integers.
Not so weird. The process is the same for other types.
Note also that the overloaded operator does not need to be a friend function in this case, since you do not need access to private members to do what you want.
@catfish666 I tought he could overload the << operator from within the class and have it output what he wanted, but I didn't really see the purpose of that so I suggest him a simple void print function... I might also add that streams are not my area of expertise :-)