问题
I worked on this all day and am stuck on my logic in how I approached this problem. Whenever I have a Vector<class> blah
. And I try to blah.push_back(class())
, it doesn't, update or work.
I've been researching and I think it has to do with pointers and references. I tried changing Vector<Student> blah
to Vector<Student*> blah
, and using 'new' Student()
when I'm pushing it back, but it still yields the same result. I think I'm defining the vectors wrong.
#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<Student> 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 &txt, vector<std::string> &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<Student> studentlist;
Vector<Course> courselist;
University() {};
void Parse(string input)
{
//Case Statements
vector<string> temp;
split( input, temp, ' ' );
cout<<temp[0];
if (temp[0]=="S ")
{
//Add Student to University
//Student* myStudent=new );
cout<<studentlist.size()<<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<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<courselist.size();i++)
{
if (courselist.at(i).course_id==atoi(temp[1].c_str()))
{
for (int j=0;j<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<courselist.size();i++)
{
if (courselist.at(i).course_id==atoi(temp[1].c_str()))
{
for (int j=0;j<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<<"--Student Report--\n";
}
else if (temp[0]=="PC")
{
cout<<"--Student Report--\n";
for (int i=0;i<courselist.size();i++)
{
for (int j=0;j<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<<first_name<<" "<<last_name<<" "<<major<<"\n";
}
}
}
}
};
int main(int argc, char *argv[])
{
string input;
while(1==1)
{
cout<<"Command:: ";
getline(cin,input);
//cout<<input;
University myUniversity;
myUniversity.Parse(input); //Input is in the format X Name Name etc etc. Arguments separated by spaces
cout<<"DEBUG:"<<myUniversity.studentlist.size();
}
return 0;
}
回答1:
Simple Error! Can't believe you spent so long on this! Haha.
You defined your University inside your while loop!
I'm not familiar with C++ (learning myself), but I think it should also be done using pointers and references and the keyword 'new'.
Can someone explain the difference between the way he's doing it and other ways I've seen where people use Student mystudent=new Student(); etc etc
来源:https://stackoverflow.com/questions/9014050/whats-wrong-with-my-logic-my-vector-or-an-object-doesnt-push-back-a-new-objec