-1

Possible Duplicate:
How to pass objects to functions in C++?

Main class

#include "List.h"
#include "Car.h"
#include "Worker.h"
#include "Queue.h"
#include <iostream>
#include <string>

using namespace std;
void initWorkerList(List<Worker>);
void initCarList(List<Car>, Queue, Queue);

int main() {
    List<Worker> WorkerList;
    List<Car> CarList;
    Queue q1, q2;
    initWorkerList(WorkerList);
    initCarList(CarList, q1, q2); // Error here
        //..... e.g cout << "Successful!"; but it does not displays it...
}

void initWorkerList(List<Worker> WorkerList) {
    Worker w1 = Worker("Ben Ang", "Ben123", "pass123", 'M');
    WorkerList.add(w1);
    Worker w2 = Worker("Grace Eng", "Gr4ce", "loveGrace", 'W');
    WorkerList.add(w2);
    Worker w3 = Worker("Rebecca Xuan", "Xuanz", "Rebecca Xuan", 'W');
    WorkerList.add(w3);
}

void initCarList(List<Car> CarList, Queue q1, Queue q2) {
    Car c1 = Car("SJS1006Z","Toyota", "Saloon car");
    Car c2 = Car("SFW6666E", "hyundai", "Taxi (Saloon)");
    Car c3 = Car("SCF1006G","Mercedes", "Large Van");
    Car c4 = Car("SBQ1006Z", "Puma", "Saloon Car");

    q1.enqueue(c1);
    q2.enqueue(c1);
    q2.enqueue(c3);
    q1.enqueue(c4);
    q1.enqueue(c1);
    q1.enqueue(c1);
    q1.enqueue(c1);
    q2.enqueue(c2);
    q2.enqueue(c2);
}

There is no error at all. But nothing is displayed when being debugged...I have tried and my guess is there is something wrong with initCarList(CarList,q1,q2); cause after that code, other codes can work at all. Is there anything wrong with it? Thanks

Community
  • 1
  • 1
unknown
  • 57
  • 2
  • 8
  • 1
    Umm...considering you are not printing anything it seems like reasonable behavior to me... You're also taking your function arguments by value, so you're modifying a copy that ceases to exist when the function returns. – Ed S. Jan 13 '13 at 06:49
  • Have you compiled it with -g? –  Jan 13 '13 at 06:50
  • 1
    Try reading: [How to pass objects to functions in C++?](http://stackoverflow.com/questions/2139224/how-to-pass-objects-to-functions-in-c) – WhozCraig Jan 13 '13 at 06:53
  • @Sandeep compiled it with -g? is that a website or application? – unknown Jan 13 '13 at 06:55

3 Answers3

5

You are passing the Queue Variables by value rather than by reference.

initCarList(CarList, q1, q2); // Error here

So any change in initCarList wont get reflected back to caller

Change your function signature to

void initCarList(List<Car> CarList, Queue& q1, Queue& q2) {

and the declaration to

void initCarList(List<Car>, Queue&, Queue&);

If you pass parameter by value, any change within initCarList is local to the function scope and does not get reflected back.

Pass by Value
Caller              Callee

|------|            |------|
workedList          workedList
| ___  |            | ___  |
||   | |-------->   ||   | | <------
||___| |            ||___| |       |
|      |            |      |       |
|q1    |            |q1    |     (Changing any of these variables 
| ___  |            | ___  |      won't be reflected back)
||   | |-------->   ||   | |       |
||___| |            ||___| |       |
|      |            |      |       |
|q2    |            |q2    |       |
| ___  |            | ___  |       |
||   | |-------->   ||   | |       |
||___| |            ||___| | <------
|______|            |______|


Pass by reference

Caller              Callee

--------            --------
|wList |            |wList |
| ___  |            | ____ |
||   | |-------->   ||    || <------
||___|<|------------||-*__||       |
|   _  |            |      |       |
|q1    |            |q1    |     (Changing any of these variables 
| ___  |            | ____ |       will be reflected back)
||   | |-------->   ||    ||       |
||___|<|------------||-*__||       |
|      |            |      |       |
|q2    |            |q2    |       |
| ___  |            | ____ |       |
||   | |-------->   ||    ||       |
||___|<|------------||-*__|| <------
|______|            |______|
Abhijit
  • 62,056
  • 18
  • 131
  • 204
2

You're passing your variables in by value, which means that the function's parameters hold a copy of them, which you modify and discard when the function ends. Pass by reference instead to modify the original variable. For example, initCarList would become:

void initCarList(List<Car> CarList, Queue &q1, Queue &q2)

You also don't use the CarList parameter, so you might as well take it out if this is how it is in your code.

chris
  • 60,560
  • 13
  • 143
  • 205
2

Your functions pass by value which means functions make a copy of passed in variables and manipulate on the copied ones. To modify on original ones you need to pass parameter by reference

Change:

void initWorkerList(List<Worker>);
void initCarList(List<Car>, Queue, Queue);

To:

void initWorkerList(List<Worker> &);
void initCarList(List<Car>&, Queue&, Queue&);
billz
  • 44,644
  • 9
  • 83
  • 100