So here is the function I'm having push_back problems with:
void School::PushBackS(ifstream &in) {
string a, b, c, d;
while(getline(in, a)) {
getline(in, b);
getline(in, c);
getline(in, d);
t.SetStudentID(a);
t.SetName(b);
t.SetAddress(c);
t.SetPhone(d);
students.push_back(Student(a, b, c, d));
cout << t.ToStringS();
}
}
Here is the main implementation (I don't think there's a problem here, but just in case):
ifstream in;
School s;
in.open(argv[1]);
if(in.is_open()) {
s.PushBackS(in);
}
in.close();
I have tried pushing back Object t, and pushing back new Student() as well, and neither work.
It is intended to read in from a file and push it back into an object. Everything works perfectly, but it does not push_back into the students vector.
In case this is important: the function PushBackS() is in a class, which calls a separate class that has the ToStringS() and Set functions, there doesn't seem to be any communication issues between classes and main because it prints out the ToString with no problem. Only issue seems to be with the push_back itself.
Thanks for your help. Sorry my formatting is a little weird.
Update: So a really weird thing is happening. Entering this function clears my first two vectors, and leaves the third alone..
void School::CalcGPA(vector<Grade> &myVector, vector<double> &gpa, vector<string> names) {
//opening this function clears my Student and Grade vectors, Query vector remains populated
Grade temporary;
for(size_t i; i < query.size(); i++) {
myVector.clear();
for(size_t j; j < grade.size(); j++) { //segmentation fault on this line
if(query[i].GetNum() == grade[j].GetNumber()) {
cout << grade[j].ToStringG();
temporary.GetGrade();
myVector.push_back(temporary);
}
if (myVector.size() != 0) {
for(size_t k = 0; k < myVector.size(); k++){
temporary = myVector[k];
temporary.GPA(gpa[i]);
}
gpa[i] = gpa[i]/myVector.size();
}
else {
gpa[i] = 0.0;
}
}
if(query.size() == gpa.size() == names.size()) {
cout << query[i].ToStringQ() << "\t" << gpa[i] << "\t" << names[i] << endl;
}
else {
cout << "error, vector sizes do not match" << endl;
}
}
}
Does anyone have any idea what is happening? I know that right before this function is opened all my vectors are fully populated, and right after, they're not. I've never had something like this before. I am using g++ to compile, if that might be the issue.