简体   繁体   中英

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;
    on->dck.clear();
    tw->dck.clear();
    th->dck.clear();
    fo->dck.clear();
    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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM