HC1122
HC1122

Reputation: 414

No value in vector when trying to display

I'm having some troubles with my current project. I have class of Jobs, which are saved inside vector. When I try to push_back or pop_back job my program works well (I can delete only as many times, as many times I add job). But when I try to display what is inside my vector I get message: Nothing to display, vector is empty, altought I push_back some jobs.

void Job::generirateActivities(vector<Job> job_list) {

  Smt s1("Default1", 1, 2, 3);
  Smt s2("Default2", 2, 3, 4);

  Date d1(1, 1, 2001);
  Date d2(2, 2, 2002);

  Job j1(&s1, &d1);
  Job j2(&s2, &d2);

  job_list.push_back(a1);
  job_list.push_back(a2); 
}

And for printing I'm using:

void Job::printJobs(vector<Job> job_list) {
  if (job_list.empty())
    cout << "Nothing to display, vector is empty." << endl;
  else {
    for (unsigned int i = 0; i < job_list.size(); i++)
      cout << i + 1 << ". " <<job_list[i].toString() << endl;
  }

What could be wrong? Any suggestions?

Thanks.

Upvotes: 0

Views: 92

Answers (1)

Sorin
Sorin

Reputation: 11968

In your code you have

void Job::generirateActivities(vector<Job> job_list) {

This means that when you call the function you make a copy of the vector and pass it to the function. You add values to it but you are actually adding to the copy, which is destroyed when you return.

You should modify it to

void Job::generirateActivities(vector<Job>& job_list) {

Note the & after vector.

Upvotes: 3

Related Questions