Sep 30th 2011, 14:11:10
So I'm working on assignments where we have to define a class with a default constructor, two argument constructor, destructor, assignment operator etc.... as well as the implementation file for it and then use the instructors test.cpp which houses int main() to run it. I was going along fine until I got to the part where I have to do the << operator for outputting an ID object which only has two variables. name_ which is a dynamically allocated string (char*) and age_ which is just an int and holds a number value.
In my header file I have:
std::ostream& operator << ( std::ostream& os, const ID& id );
now in my implementation I'm stuck as to how to output this. I can't just put os << id.name_ << ' ' << id.age_; because std::ostream& is not a member function of the class (and the professor does not want us to declare it as a friend). Any thoughts on how I'd go about doing this?
In my header file I have:
std::ostream& operator << ( std::ostream& os, const ID& id );
now in my implementation I'm stuck as to how to output this. I can't just put os << id.name_ << ' ' << id.age_; because std::ostream& is not a member function of the class (and the professor does not want us to declare it as a friend). Any thoughts on how I'd go about doing this?
Smarter than your average bear.