Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat's wrong with my logic? My Vector or an object doesn't push_back a new object. Stays at size of 0
    primarykey
    data
    text
    <p>I worked on this all day and am stuck on my logic in how I approached this problem. Whenever I have a <code>Vector&lt;class&gt; blah</code>. And I try to <code>blah.push_back(class())</code>, it doesn't, update or work.</p> <p>I've been researching and I think it has to do with pointers and references. I tried changing <code>Vector&lt;Student&gt; blah</code> to <code>Vector&lt;Student*&gt; blah</code>, and using 'new' <code>Student()</code> when I'm pushing it back, but it still yields the same result. I think I'm defining the vectors wrong.</p> <pre><code>#include "std_lib_facilities.h" using namespace std; class Student { public: string first_name; string last_name; string major; int student_id; Student() {}; Student(int no, string name1, string name2, string majorin) { first_name = name1; last_name = name2; student_id = no; major = majorin; } }; class Course { public: Vector&lt;Student&gt; Students; int max_enrollment; int course_id; string course_title; Course() {}; Course(int no_in,int max_in,string title_in) { max_enrollment=max_in; course_title=title_in; course_id=no_in; } }; unsigned int split(const std::string &amp;txt, vector&lt;std::string&gt; &amp;strs, char ch) { unsigned int pos = txt.find( ch ); unsigned int initialPos = 0; strs.clear(); // Decompose statement while( pos != std::string::npos ) { strs.push_back( txt.substr( initialPos, pos - initialPos + 1 ) ); initialPos = pos + 1; pos = txt.find( ch, initialPos ); } // Add the last one strs.push_back( txt.substr( initialPos, std::min( pos, txt.size() ) - initialPos + 1 ) ); return strs.size(); } class University { public: Vector&lt;Student&gt; studentlist; Vector&lt;Course&gt; courselist; University() {}; void Parse(string input) { //Case Statements vector&lt;string&gt; temp; split( input, temp, ' ' ); cout&lt;&lt;temp[0]; if (temp[0]=="S ") { //Add Student to University //Student* myStudent=new ); cout&lt;&lt;studentlist.size()&lt;&lt;endl; //Student(atoi(temp[1].c_str()),temp[2],temp[3],temp[4]) studentlist.push_back(Student()); } else if (temp[0]=="C") { //Add Course to University Course myCourse=Course(atoi(temp[1].c_str()),atoi(temp[2].c_str()),temp[3]); courselist.push_back(myCourse); } else if (temp[0]=="L") { //Add Student to Course list //Not Implemented-Find Course by temp[1] which is a int for (int i=0;i&lt;courselist.size();i++) { if (courselist.at(i).course_id==atoi(temp[1].c_str())) { courselist.at(i).max_enrollment=atoi(temp[1].c_str()); } } } else if (temp[0]=="A") { for (int i=0;i&lt;courselist.size();i++) { if (courselist.at(i).course_id==atoi(temp[1].c_str())) { for (int j=0;j&lt;studentlist.size();j++) { if (studentlist.at(j).student_id==atoi(temp[1].c_str())) { Student mystudent=studentlist.at(j); courselist.at(i).Students.push_back(mystudent); } } } } } else if (temp[0]=="D") { for (int i=0;i&lt;courselist.size();i++) { if (courselist.at(i).course_id==atoi(temp[1].c_str())) { for (int j=0;j&lt;courselist.at(i).Students.size();j++) { if (courselist.at(i).Students.at(j).student_id==atoi(temp[1].c_str())) { //Student mystudent=courselist.at(i).Students.at(j); courselist.at(i).Students.erase(courselist.at(i).Students.begin()+j); } } } } } else if (temp[0]=="PS") { cout&lt;&lt;"--Student Report--\n"; } else if (temp[0]=="PC") { cout&lt;&lt;"--Student Report--\n"; for (int i=0;i&lt;courselist.size();i++) { for (int j=0;j&lt;courselist.at(i).Students.size();j++) { string first_name=courselist.at(i).Students.at(j).first_name; string last_name=courselist.at(i).Students.at(j).last_name; string major=courselist.at(i).Students.at(j).major; //Waiting List var cout&lt;&lt;first_name&lt;&lt;" "&lt;&lt;last_name&lt;&lt;" "&lt;&lt;major&lt;&lt;"\n"; } } } } }; int main(int argc, char *argv[]) { string input; while(1==1) { cout&lt;&lt;"Command:: "; getline(cin,input); //cout&lt;&lt;input; University myUniversity; myUniversity.Parse(input); //Input is in the format X Name Name etc etc. Arguments separated by spaces cout&lt;&lt;"DEBUG:"&lt;&lt;myUniversity.studentlist.size(); } return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload