Ok, I have the following code:
Now then, this code should (from my test application) print out zero, add the assignment passed to the function to a vector of assignments, and then prinout the size again. This works great.Code:void student::add_assignment(assignment *hw) { std::cout<<"Size of assignment vector right now:"<<this->homework.size()<<"\n"; this->homework.push_back(*hw); std::cout<<"Size of assignment vector now:"; std::cout<<this->homework.size()<<"\n"; }
However, in another call to homework.size(), which is called directly after the above code is called, homework is reported as being of size 0 (that is, homework.size() returns 0), and thus creates an abnormal program termination after trying to return the assignment at the position passed to the function.
Here is that code:
In other words: at the end of the first function, the vector homework is of size 1 (that is, it has one element in it). However, at the beginning of the second function, it is reported to be of size 0, and thus always returns the temp assignment created in it.Code:assignment student::get_assignment(const int a) { std::cout<<"Size of assignment vector is:"; std::cout<<this->homework.size(); std::cout<<"\n"; assignment *temp = new assignment(0,true,'a',22); if(a > homework.size() || homework.empty()) { return *temp; } else { return homework.at(a); } }
Basicly, I want to know why the first function doesn't add the element to the vector for the variable that holds the vector.
Here is how the code is called:
Any ideas? If you need any more of the code just ask for it.Code://From main.cpp #include <cstdlib> #include <iostream> #include "course.h" #include "student.h" #include "assignment.h" using namespace std; course *asdf = new course("asdf", 1, "me"); int main(int argc, char *argv[]) { student *xyz = new student(); asdf->add_students(xyz); asdf->get_student(0).add_assignment(new assignment(90505, true, 'a', 0)); long g = asdf->get_student(0).get_assignment(0).get_duedate(); std::cout<<g; std::cin>>g; return 0; }



LinkBack URL
About LinkBacks


