title: I/O overloading and reading from text files
body: <p>I need to define a read and a print function for a class that has an array of objects as a private variable. I have to read in objects from a text file and print them to the screen. To do this I need to overload the &lt;&lt; and >> operators. I understand I need to use loops to read and print the information stored in the array but I'm not sure how to accomplish this. My lecturer has given us a skeleton code which is basically function prototypes and the main function which I need to stick to. I understand how this works with public structs as I have done this exact scenario using that but the private variables of class' are tripping me up. </p>
  
  <pre><code>class EmployeeList {
  public:
    //Constructors
    EmployeeList();
    EmployeeList(istream&amp;);
    //Accessors
    bool isEmpty() const;
    bool isFull() const;
    int size() const; //Number of employees in list
    Employee item(int i) const; //i'th employee
    //Mutators
    void setItem(int i,const Employee&amp; e);
    //I/O functions, sets the i'th emplyee to e
    void read(istream&amp;);
    void print(ostream&amp;) const;
  
  private:
    enum {MAXSIZE = 100};
    Employee list[MAXSIZE];
    int count; //Number of employees in the current list
  };
  
  EmployeeList::EmployeeList() {
    count = 0;
  }
  
  EmployeeList::EmployeeList(istream&amp; in) {
    //list[MAXSIZE] = in;
  }
  
  bool EmployeeList::isEmpty() const {
    return (count == 0);
  }
  
  bool EmployeeList::isFull() const {
    return (count == MAXSIZE);
  }
  
  int EmployeeList::size() const {
    return count;
  }
  
  Employee EmployeeList::item(int i) const {
  }
  
  void EmployeeList::setItem(int i, const Employee&amp; e) {
  }
  
  void EmployeeList::read(istream&amp; in) {
    Employee tempList;
    while (in &gt;&gt; tempList) {
    }
  }
  
  void EmployeeList::print(ostream&amp; out) const {
    for (int i=0; i &lt; size(); i++) {
    }
  
    cout &lt;&lt; out;
  }
  </code></pre>
  
  <p>The above part is the Class EmployeeList while the below part are overloading functions. The commented out parts are ideas that I thought might work but didn't. </p>
  
  <pre><code>istream&amp; operator&gt;&gt;(istream&amp; in, EmployeeList&amp; l) {
    l.read(in);
    return in;
  }
  
  ostream&amp; operator&lt;&lt;(ostream&amp; out, const EmployeeList&amp; l) {
    l.print(out);
    return out;
  }
  </code></pre>
  
  <p>Below is the main function given to us. </p>
  
  <pre><code>int main() {
    authorInfo();
    ifstream infile("a1in.txt");
    if(!infile) {
        cout &lt;&lt; "file 'alin.txt' not found.";
        return EXIT_FAILURE;
    }
    EmployeeList theList(infile);
  
    cout &lt;&lt; endl;
    cout &lt;&lt; theList.size() &lt;&lt; " employees read:\n" &lt;&lt; theList &lt;&lt; endl;
    process(theList);
    return EXIT_SUCCESS;
  
  }
  </code></pre>
  
  <p>Hope someone can steer me in the right direction! Let me know if you need more of the code. Thanks!</p>
  
  <p>EDIT: 
  Employee read and print functions:</p>
  
  <pre><code>void Employee::read(istream&amp; in) {
    in &gt;&gt; name &gt;&gt; id &gt;&gt; salary;
  }
  
  void Employee::print(ostream&amp; out) const {
    out &lt;&lt; getName() &lt;&lt;" "&lt;&lt; getID() &lt;&lt;" "&lt;&lt; getSalary() &lt;&lt; endl;
  }
  </code></pre>
  
  <p>Employee overloading:</p>
  
  <pre><code>istream&amp; operator&gt;&gt;(istream&amp; in, Employee&amp; e) {
    e.read(in);
    return in;
  }
  
  ostream&amp; operator&lt;&lt;(ostream&amp; out, const Employee&amp; e) {
    e.print(out);
    return out;
  }
  </code></pre>
  
  <p>EDIT 2: Updated read() function. The line with the while is where the error is.</p>
  
  <pre><code>void EmployeeList::read(istream&amp; in) {
    Employee inEmployee;
    while (in &gt;&gt; inEmployee &amp;&amp; count &lt; MAXSIZE) {
        list[count] = inEmployee;
        count++;
    }
  }
  </code></pre>
  
  <p>EDIT 3: Here is the print() function I have so far. It does indeed print but I get the default constructor information rather than information from the file. Is this a read or print function issue? I'm thinking read function still. </p>
  
  <pre><code>void EmployeeList::print(ostream&amp; out) const {
    cout &lt;&lt; endl;
    for (int i=0; i &lt; count; i++) {
        out &lt;&lt; list[count];
    }
  } 
  </code></pre>