b0b3k b0b3k - 24 days ago 5x
C++ Question

Empty vectors after split

I'm trying write a simple application based on card trick. In some part of that app i have to split alternately a main deck to smaller decks. I wrote a simple loop to do that, but after invoking decks are still empty. I paste that part of code below. Do you know where i made mistake? Of course if you want check whole program you could do it here. That method is invoked by "main" instance of Deck class, smaller deck are parameters of that method.

void split_deck(Deck one, Deck two, Deck three, Deck four, int time)
Deck* on=&one;
Deck* tw=&two;
Deck* th=&three;
Deck* fo=&four;
int buff;

for (int i = 0; i < 24; i++)
buff = dck[i];
if (i%time == 0) on->dck.push_back(buff);
if (i%time == 1) tw->dck.push_back(buff);
if (i%time == 2) th->dck.push_back(buff);
if (i%time == 3) fo->dck.push_back(buff);


to expand on Nathan's comment, you should pass the vectors by reference:

 void split_deck(Deck& one, Deck& two, Deck& three, Deck& four, int time)

this way the changes will be preserved and be visible to the caller. the way you pass the vectors now (by value) you're essentially working on their copies inside the function, the actual vectors outside the function are not changed.

and yes, please get rid of those pointers ( i assume they were there for test anyway) and use the vectors you pass to the function directly