I am trying to over load the insertion operator to work with a class. I want to use get() to load a character array inside the operator but I am having trouble. Here is a simple example to illustrate what I am trying to do in a larger program.
I have tried this a few different ways, but obviously cin is not a member of Person so that's pretty much my problem. Am I just going to have write a member function to load this array or is there any other way around this. Thanks. MSVC++6.0Code:#include <iostream.h> const int MAX = 30; class Person { private: int age; char gender; char name[MAX]; public: friend istream& operator >> (istream&, Person&); friend ostream& operator << (ostream&, Person&); }; istream& operator >> (istream& in, Person& x) { cout <<"\nEnter Age:"; in >> x.age; cout <<"\nEnter Gender:"; in >> x.gender; cout <<"\nEnter Name:"; cin.ignore(); x.cin.get(name,MAX); return in;} ostream& operator << (ostream& out, Person& x) { cout << "\nAGE:"; out << x.age; cout << "\nGENDER:"; out << x.gender; cout << "\nNAME:"; out << x.name; return out;} int main() { Person p; cin >> p; cout << p; return 0; }



LinkBack URL
About LinkBacks


