std::list::push_back is deleting my list member variables

Viewed 89

I am currently working through a problem from a previous semester in school, and I'm having problems with the push_back() function for the standard library std::list. I have a functioning copy constructor, as demonstrated by the test variable correctly copying the variable s.

For some reason, whenever the push_back() function is called for a std::list of my custom class, all the list member variables are ignored. I am not supposed to make any changes to main.cpp, and in general I am curious as to why the list::push_back() is ignoring my member variable lists, as I had believed that push_back() simply copied data values and my copy constructor is working.

main.cpp function

// read the data for a school & add to the schools list
void AddSchool(std::istream &istr, std::list<School> &schools) {
  std::string school_name, student_name;
  int num_slots, num_prefs;
  istr >> school_name >> num_slots >> num_prefs;
  // create the school
  School s(school_name, num_slots);
  for (int i = 0; i < num_prefs; i++) {
    istr >> student_name;
    // add the students in the order listed
    s.AddStudent(student_name);
  }
  // add to the schools list
  School test(s);
  schools.push_back(s);
}

lldb debugging of main.cpp function

(lldb) next
Process 69781 stopped
* thread #1, queue = 'com.apple.main-thread', stop reason = step over
    frame #0: 0x0000000100002269 main`AddSchool(istr=0x00007ffeefbff5c8, schools=size=1) at main.cpp:31:1
   28     // add to the schools list
   29     School test(s);
   30     schools.push_back(s);
-> 31   }
   32  
   33   // read the data for a student & add to the students list
   34   void AddStudent(std::istream &istr, std::list<Student> &students) {
Target 0: (main) stopped.
(lldb) p schools
(std::list<School, std::allocator<School> >) $0 = size=1 {
  [0] = {
    school_name = "university_of_michigan"
    num_slots = 3
    preferences = size=0 {}
    accepted = size=0 {}
    prfptr = {
      __ptr_ = 0x0000000000000000
    }
  }
}
(lldb) p test
(School) $1 = {
  school_name = "university_of_michigan"
  num_slots = 3
  preferences = size=3 {
    [0] = "erin_jones"
    [1] = "john_smith"
    [2] = "dave_roberts"
  }
  accepted = size=0 {}
  prfptr = {
    __ptr_ = 0x0000000000000000
  }
}
(lldb) 

school.h

#ifndef __school_h_
#define __school_h_
#include <string>
#include <list>

class School {
public:
  //Constructors
  School (std::string school_name_, int num_slots_) 
  : school_name(school_name_), num_slots(num_slots_)
  {}
  School (const School &s);

  //Modifiers
  void AddStudent(std::string student_name) { preferences.push_back(student_name); }
  void PrepareToMakeOffers() { prfptr = preferences.begin(); }
  void StudentTentativelyAcceptsOffer(std::string student) { accepted.push_back(student); }
  void StudentDeclinesTentativeAcceptance(std::string student);

  //Accessors
  std::string GetName() const { return school_name; }
  int MaxAcceptedStudents() const { return num_slots; }
  int NumAcceptedStudents() const { return accepted.size(); }
  std::list<std::string>::const_iterator PreferredStudentList() const { return preferences.begin(); }
  int NumPreferences() const { return preferences.size(); }
  std::string MakeNextOffer();

private:
  std::string school_name;
  int num_slots;
  std::list<std::string> preferences, accepted;
  std::list<std::string>::iterator prfptr;
};

#endif

school.cpp

#include <string>
#include <list>
#include "school.h"

School::School (const School &s) {
    this->school_name = s.school_name;
    this->num_slots = s.num_slots;
    this->preferences = s.preferences;
}

std::string School::MakeNextOffer() {
  std::string offer = *prfptr;
  prfptr++;
  return offer;
}

void School::StudentDeclinesTentativeAcceptance(std::string student) {
  std::list<std::string>::iterator acptptr = accepted.begin();
  while(acptptr != accepted.end() && *acptptr != student) {
    acptptr++;
  } if (*acptptr == student) accepted.erase(acptptr);
}

Note: I re-uploaded this question cause my previous thread was closed.

0 Answers
Related