我的逻辑有什么问题?我的矢量或对象不会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
我一整天都在做这件事,在处理这个问题的过程中,我一直坚持自己的逻辑。每当我有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()的方式之间的区别吗;等等
相关文章:
- 模板,函数使用错误的构造函数来复制我的对象
- 为什么C++无法识别我的对象实例化?
- 为什么当我尝试返回引用时,我的对象仍然被复制
- 为什么我的对象声明不调用默认构造函数?
- 为什么Visual Studio会以不同的方式对待我的对象
- 从我的对象返回静态数组
- 为什么我的对象似乎在不使用new”的情况下在堆上
- 我应该如何声明我的对象才能正确使用这些函数
- 为什么我的对象似乎被创建了两次
- 是否可以在我的对象上停止 std::addressof
- 为什么为我的对象类编译标准优先级队列失败
- STL 列表迭代器不会更新我的对象
- C++-如果我使用映射,我的对象会被删除两次吗
- 不完整的类型 --> 无法从我的对象访问指针值
- 如何停止std/boost::线程复制我的对象而不是传递引用
- 为什么我不能退回我的对象
- 为什么我的对象中的变量被重置为默认值
- 如何为我的对象重载 cout
- 析构函数,不会删除我的对象
- 将我的对象与字符串连接