-2

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.

tydcghk
  • 1
  • 3
  • What makes you think `push_back` does not work? What's the input? What's the expected output? What's the actual output? – R Sahu Jul 11 '17 at 04:44
  • Input is a text file, there are two expected outputs: first, the cout should print out each object in the formatting I want, this works great; second, the objects should push back into my students vector, but when I check the vector size it comes out zero, and when I try to print directly from the vector, nothing comes out. – tydcghk Jul 11 '17 at 04:58
  • That all sounds good. Please post a [mcve]. Without that, it's hard to figure out where the problem could be. – R Sahu Jul 11 '17 at 05:10
  • I'm not sure what those mean.. But I looked it up, and I think it's shallow? My constructor is set up as: this->string = string; Does that tell you? – tydcghk Jul 11 '17 at 06:18
  • Okay, so I tried to do the Minimal, Complete, and Verifiable example, and it IS pushing back, it's just losing them later, and I have absolutely no idea how. I'll update with the code that's losing the data. – tydcghk Jul 11 '17 at 07:50

1 Answers1

0

I suspect that the gpa array is not sized properly and the statement

gpa[i] = 0.0;

causes memory corruption.

You should use the at() function instead of the [] operator because it performs bounds checking. The vector<string> argument is suspicious as well; it should probably be const vector<string>&, so that no copy is made.

Mike Kinghan
  • 55,740
  • 12
  • 153
  • 182
Florian Weimer
  • 32,022
  • 3
  • 48
  • 92