I got a problem,
class student {
public:
static int id,a,c;
static bool editable;
static std::queue <int> classes;
static queue <int> classesT;
static queue <int> classesD;
public:
student(int x,int y,queue <int> z){
editable=true;
id=x;
a=y;
classes=z;
c=classes.size();
}
void DisposeObject()
{
delete this;
}
};
int main(){
std::vector <queue <int> >links;
std::vector <int> quotas;
std::vector <student> sList;
std::queue <int> classes;
std::queue <student> q1;
int a,c,sNum,cNum;
static int temp;
ifstream myFile("....");
if(myFile.is_open()){
myFile>>sNum;
myFile>>cNum;
}
for(int i=0;i<sNum;i++){
myFile>>c;
myFile>>a;
for(int j=0;j<c;j++){
myFile>>temp;
classes.push(temp);
}
student *s1=new student(i,a,classes);
sList.push_back(*s1);
s1->DisposeObject();
while(!classes.empty())
classes.pop();
}
This is my code. I want to add different objects to my vector but whenever a new student created students that created before take its values and I end up with a vector something like:
a,a,a,a
instead of
a,b,c,d
By the way I already tried reserve function.
myFileduring each iteration? If you end up with four instances ofstudentin the vector, it means that thepush_backs work but the information stored with the objects is incorrect. Also, why do younewthe student? You can just as easilypush_backa temporary and spare yourself the clean-up:sList.push_back(student(i,a,classes));Also, doesDisposeObject()commit sucicide, i.e. does it dodelete this? Otherwise, you have a leak with every singlestudentyounewup. (Note: I don't recommend suicide!).delete s1?studentcopyingclasses? It seems like if you just declarevector<whatever> classesinside yourfor(int i...)loop, you'd get a fresh one each iteration. Yourwhile() poploop just seems either unnecessary or wrong.