-2

I saw many posts here but none of them explains what I'm trying to do. I have the vector vector <Car*> carVector; and I want to pass it to a function so I can read the information of the objects in there. The objective is to pass it and then I can use it in this cicle

for (i = 0; i < racetrack.size(); i++) {
    for (int j = 1; j < nRaceTracks + 1; j++) {
        if (racetrack[i]->getNome() == info[j]) {
            size= racetrack[i]->getTrackSize() / 100;
            for (int c = 0; c < carVector.size(); c++)
                carVector[c]->chargeBattery(500);
        }
    }
}
whoami
  • 1
  • 3
  • 3
    What problem are you encountering? Please show the function and what you tried. – Nico Schertler Nov 29 '19 at 16:55
  • `void Foo(vector carVector) { ...` – Eljay Nov 29 '19 at 16:57
  • You have to be more specific. There is nothing special by passing a vector of pointers. – drescherjm Nov 29 '19 at 16:57
  • The function that is receiving the vectors is `void Campeonato::criaCampeonato(string* info, vector *carVector, vector *racetrack)` but I don't know if I'm passing the vectors in the correct way – whoami Nov 29 '19 at 16:58
  • Why are you are you passing a pointer to a vector? Pass the vector itself, either by value, or by reference (if you want the caller to see the change). You could also pass by constant reference if you don't want to copy or modify the vector. – drescherjm Nov 29 '19 at 16:59
  • BTW, The reason you are being downvoted is you did not post a [mcve] – drescherjm Nov 29 '19 at 17:02
  • @drescherjm how do I do that? I just want to be able to read the information from the vector in the function that receives it, not changing values. – whoami Nov 29 '19 at 17:02
  • Then you probably want const reference. The answers already explained that. – drescherjm Nov 29 '19 at 17:03
  • Thanks, I understood the problem – whoami Nov 29 '19 at 17:11

2 Answers2

1

For the function you've mentioned, just pass the vector by value, reference or const reference depending upon the circumstance, like following:

  • Value: void Campeonato::criaCampeonato(string* info, vector<Carro*> vectorCarro, vector<Autodromo*> *vectorAutodromo)

  • Reference: void Campeonato::criaCampeonato(string* info, vector<Carro*>& vectorCarro, vector<Autodromo*> *vectorAutodromo)

  • Const reference: void Campeonato::criaCampeonato(string* info, const vector<Carro*>& vectorCarro, vector<Autodromo*> *vectorAutodromo)

If you must pass it as pointer then you can just dereference it within the function to use it as an array as follows:

void Campeonato::criaCampeonato(string* info, vector<Carro*> *vectorCarro, vector<Autodromo*> *vectorAutodromo) {
  for (i = 0; i < racetrack.size(); i++) {
    for (int j = 1; j < nRaceTracks + 1; j++) {
        if (racetrack[i]->getNome() == info[j]) {
            size= racetrack[i]->getTrackSize() / 100;
            for (int c = 0; c < carVector.size(); c++)
                (*vectorCarro)[c]->chargeBattery(500);
        }
    }
  }
}
walnut
  • 21,629
  • 4
  • 23
  • 59
Saisai3396
  • 311
  • 1
  • 8
1

From the snippet you gave in the comments, you are trying to pass in pointers. You do not want to do this. You want references instead:

void Campeonato::criaCampeonato(string *info, const vector<Car*> &carVector, const vector<racetrack*> &racetrack) {
    for (size_t i = 0; i < racetrack.size(); i++) {
        for (int j = 1; j < nRaceTracks + 1; j++) { // you will need to pass nRaceTracks in too
            if (racetrack[i]->getNome() == info[j]) {
                size= racetrack[i]->getTrackSize() / 100;
                for (size_t c = 0; c < carVector.size(); c++)
                    carVector[c]->chargeBattery(500);
            }
        }
    }
}
ChrisMM
  • 8,448
  • 13
  • 29
  • 48