L.Moyer
L.Moyer

Reputation: 81

Deque iterator not dereferenceable

So I've been attempting file input and output to save Yugioh decklists, and I got it working for the most part. I am using deques to store a class object known as Card that contains all the information, and a Deck class to hold deques for Card objects for each type of deck, main, side, and extra.

So anyway, onto the problem. I am using a local deque to hold information on previously loaded cards, as when I load each card, I search the deque for exact matches and then store the number inside a variable for use in output. When I attempt to add to this local deque, called "processed", I get an error.

Debug Assertion Failed! Expression: deque iterator not dereferenceable

void Deck::SaveDeck(std::string filename)
{
    std::ofstream outFile;
    outFile.open(filename.c_str());

    //send the file the deck name
    outFile << this->deckName << "\n";

    //send the deck sizes
    outFile << main_deck_size << " " << side_deck_size << " " << extra_deck_size << "\n";

    //iterator to iterate through the entire deck looking for copies
    std::deque<Card>::iterator iter = main_deckCards.begin();
    std::deque<Card>::iterator iter2 = main_deckCards.begin();

    //deque to hold previously added cards
    std::deque<Card> processed;
    std::deque<Card>::iterator processed_iter = processed.begin();

    string setNAME = "";
    int setNum = 0;
    int quantity = 0;
    bool is_processed = false;

    for (int i = 0; i < main_deck_size; i++)
    {
        //reset processed flag
        is_processed = false;

        //check processed queue for exact card
        for (int j = 0; j < processed.size(); j++)
        {
            if (iter[i].SET_NAME == processed_iter[j].SET_NAME && iter[i].SET_NUM == processed_iter[j].SET_NUM)
            {
                is_processed = true;
                break;
            }
        }

        if(is_processed == false)
        {
            //reset variables
            setNAME = "";
            setNum = 0;
            quantity = 0;

            //draw from the next card
            setNAME = iter[i].SET_NAME;
            setNum = iter[i].SET_NUM;
            quantity++;

            //loop to look for similar cards
            for (int x = i+1; x < main_deck_size; x++)
            {
                if (iter2[x].SET_NAME == setNAME && iter2[x].SET_NUM == setNum)
                {
                    quantity++;
                }
            }

            outFile << setNAME << " " << setNum << " " << quantity << "\n";

            if(setNAME == "LOB-EN")
            {
                //removing this line causes the program to work
                processed.push_back(LOB[setNum]);
            }
        }
    }
}

Removing the line where I attempt to put the object into the deque and the program runs without an error, except that it writes the same cards multiple times, which I want to handle the file with quantity for each card, instead of multiples appearing on different lines. I've already checked other questions, which the problem was due to attempting to use std::deque::pop_front/back on an empty deque. But I don't call pop_front/back at all.

Any ideas on what could be causing the error?

Upvotes: 1

Views: 752

Answers (1)

Bo Persson
Bo Persson

Reputation: 92271

Any use of processed.push_back(LOB[setNum]); might invalidate processed_iter.

On the first round in the loop, when processed starts out empty, the invalidation is more or less guaranteed.

Upvotes: 1

Related Questions