我的逻辑有什么问题?我的矢量或对象不会push_back新对象。保持大小为 0

What's wrong with my logic? My Vector or an object doesn't push_back a new object. Stays at size of 0

本文关键字:我的 对象 新对象 back 小为 什么 问题 push      更新时间:2023-10-16

我一整天都在做这件事,在处理这个问题的过程中,我一直坚持自己的逻辑。每当我有Vector<class> blah。我尝试blah.push_back(class()),但它没有更新或工作。

我一直在研究,我认为这与指针和参考文献有关。我尝试将Vector<Student> blah更改为Vector<Student*> blah,并在向后推时使用"new"Student(),但它仍然产生相同的结果。我认为我对向量的定义是错误的。

#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;
}

简单错误!真不敢相信你在这上面花了这么长时间!哈哈。

你在while循环中定义了你的大学!

我不熟悉C++(自学),但我认为它也应该使用指针和引用以及关键字"new"来完成。

有人能解释一下他做这件事的方式和我看到的其他人使用Student mystudent=new Student()的方式之间的区别吗;等等