vector - Trouble with C++ Class -


i teacher @ independent boarding school , trying to write program in c++ randomly seat students @ tables in our dining hall sit different students , different staff members each week. ideally, on given period, not sit @ same table twice , many different students possible. have created program in python , works great (well, pretty good). variety of reasons, trying port on c++ (which not know @ all) can hand off boarding staff. both student , staff (as table capacities) read text files. have created 2 custom classes, 1 students , 1 tables, handle data. here 2 class header files:

table.h

#pragma once #include <iostream> #include "student.h" #include <vector>  using namespace std;  class table {     // private class variables     string staff;     int numseats;     vector<student> seating; public:     table(); // default constructor     table(string s, int n);     table(const table& that) : staff(that.staff), numseats(that.numseats)     {     }     // copy constructor     table& operator=(const table& that)     {         staff = that.staff;         numseats = that.numseats;         return *this;     }     int getnumseats();     string getstaffname();     void addstudent(student student);     void removestudent(student student);     void clearstudents();     vector<student> gettableseating();     int getremainingseats();     ~table(void); }; 

here student class file:

#pragma once #include <iostream> #include <vector>  using namespace std;  class student {     string name;     string country;     vector<int> tablessatat; public:     student(string n, string c);     student();     student(const student& that) : name(that.name), country(that.country)     {     }     student& operator=(const student& that)     {         name = that.name;         country = that.country;         return *this;     }     string getname();     string getcountry();     void addtable(int tablenumber);     void removetable(int tablenumber);     bool satattable(int tablenumber);     friend bool operator==(student s1, student s2);     friend bool operator!=(student s1, student s2);     ~student(void); };  bool operator==(student s1, student s2); bool operator!=(student s1, student s2); 

here recursion function heavy lifting:

bool seatrecursive(vector<student> &tempstudents, vector<table> &temptables) {     if (tempstudents.size() == 0) return true; //base case      student nextstudent = randomselect(tempstudents);     (vector<int>::size_type i=0; i<temptables.size(); i++)     {         if (temptables[i].getremainingseats() > 0 && !nextstudent.satattable(i))         {             addstudenttotable(nextstudent, temptables, i);             if (seatrecursive(tempstudents, temptables)) return true;             else              {                 removestudentfromtable(nextstudent, temptables, i);                 tempstudents.push_back(nextstudent);             }         }     }     return false; } 

most of works. when run program, text file 10 weeks of seating, table seatings same. i.e. if particular staff member, have same kids sitting @ table 10 weeks. have vector of ints supposed storing table numbers student has sat @ on time. when debugging, notice table numbers not being stored in vector, empty. problem can't figure out why happening. because passing vectors reference? have pointers, though not explicitly declaring pointers?

any suggestions appreciated , can paste in rest of code if necessary.

brian

i see both copy constructors (and assignment constructors) of student , table class forget copying stl vectors contained (vector<student> seating , vector<int> tablessatat). since overload constructors should copy them through vector copy constructor not implicitly done.

without copying them, every time student or table moved around vector (or assigned temporary) internal vector discarded new object.

as pointed out useless's comment in case don't need declare them since rule of three doesn't apply: don't need destructor won't need 2 copy constructors.

a note: call copy constructor in comment copy assignment constructor instead. 1 before real copy constructor.


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -