Ok Gang,
Mr. Dummy (myself!! LOL) is back with another problem that you would all THINK is easy (I hope it is) but I'm screwing something up!
I am going through the "Principles and Practices Using C++" book as you probably know and I am in chapter 9 exercises (specifically exercise 9-3 (9-2 was successfully solved)..... and the #3 exercise says it needs me to to "provide a Global << operator" for printing my class and since overloading operators was introduced in this chapter I'm thinking - hey..... just overload the "<<" and voila!
Well, I have tried to overload the operator and it is not working to say the least! I am getting errors saying that A.) I need to use template syntax (HAVEN'T GOTTEN TO TEMPLATES YET!!) it also says B.) that ostream does NOT name a type and finally C.) "no match for operator<<.."
Things you should know==>
1.) I have DEFINED and DECLARED the operator overload OUTSIDE of my class. It looks like this==>
class Name_pairs{ //the class I define
. . . .
}; //end of class definition
Here's where the operator overload declaration is.
Followed closely by the definition here.
Please see below code for the EXACT look of these...
2.) I have #included <iostream> AND <ostream> for this code along with the usual suspects
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
//declaration is here==>
std::ostream& std::ostream::operator<<(std::ostream& os, const Name_pairs& np);
//here's the definition==>
ostream& std::ostream::operator<<(std::ostream& os, const Name_pairs& np){
for(unsigned i=0; i<np.names.size(); ++i){
os<<np.names[i]<<" "<<np.ages[i]<<"\n";
} //end for
os<<"\n";
return os;
}//end of << operator overload definition
|
Name_pairs is MY class that I defined for the exercise as directed by the author. I THINK I am also verbatim defining the operator overload based on the author's examples as well on page 337.
So what is going wrong here? Why am I getting errors when I THINK I giving the overload definition the right info based on the book's models??
Xanadu