使用map.erase后map / set不可引用

时间:2015-11-02 00:28:38

标签: c++ debugging dictionary assertion

我在我的代码中得到一个map / set而不是dereferanceable调试断言错误,我不知道如何修复它,我已经在我的Shape :: insert函数中找到了map.erase的问题但是我不知道如何阻止问题发生任何帮助,谢谢

如果我删除了擦除功能,但是我需要它来擦除地图上的项目,以便它们不再被这些功能使用

ShapeDraw.h

#ifndef _SHAPE_
#define _SHAPE_
#include <Windows.h>
#include <cmath>
#include <math.h>
#include <stdlib.h>
#include <map>
#include <algorithm>
#include <cstdlib>
#include <time.h>
#include <fstream>
#include <commdlg.h>
struct CardData
{
    int _x;
    int _y;
    LPCWSTR _filename;
    int _value;
    int _suit = 0; //0 - Hearts, 1 - Diamonds, 2 - Spaces, 3 - Clubs
    bool _face = 0; //0 - Face Down, 1 - Face Up <-- This bit is only used for adding the cards to the stacks and shit
};
struct cardStackList;
class Shape
{

public:
    cardStackList *stack[11];
    //extern cardStackList *stack[11];
    //static HBITMAP mBkg;
    int g_width = 10; //Default Pen Size is 10,
    int g_pen = PS_SOLID;
    int pCountMax = pCount;
    int pCount = 0; //Painting Count
    std::map<int, CardData> _cardMatrix;
    std::map<int, CardData>::iterator _rCardIter;
    cardStackList* make_stack();
    void insert(cardStackList *_stack);
    void CreateStacks();
    cardStackList* remove(cardStackList *_stack); //Remove the top most card
    cardStackList* search(cardStackList *_stack, int _value, int _suit);
    void display(cardStackList *_stack); //Draw Cards in descending order
    Shape()
    {
        for (int i = 0; i < 11; i++)
        {
            stack[i] = NULL;
        }
    }
};
#endif

ShapeDraw.cpp

#include "ShapeDraw.h"
struct cardStackList
{
    int _x;
    int _y;
    LPCWSTR _filename;
    int _value;
    int _suit;
    struct cardStackList *next;
    struct cardStackList *prev;
    bool _face; //0 - Face Down, 1 - Face Up
}*stack[11];
cardStackList* Shape::make_stack()
{
    int i;
    struct cardStackList *temp;
    temp = new(struct cardStackList);
    if (temp == NULL)
    {
        return 0;
    }
    else
    {
        i = rand() % (_cardMatrix.size());
        _rCardIter = _cardMatrix.find(i);
        temp->_x = _rCardIter->second._x;
        temp->_y = _rCardIter->second._y;
        temp->_filename = _rCardIter->second._filename;
        temp->_value = _rCardIter->second._value;
        temp->_suit = _rCardIter->second._suit;
        temp->_face = _rCardIter->second._face;
        temp->next = NULL;
        temp->prev = NULL;
        _cardMatrix.erase(_rCardIter);
        pCount--;
        pCountMax = _cardMatrix.size() + 1;
    }
    return temp;
}
void Shape::insert(cardStackList *_stack)
{
    struct cardStackList *temp, *s, *t;
    temp = make_stack();
    s = _stack;
    t = s;
    if (_stack == NULL)
    {
        _stack = temp;
        _stack->next = NULL;
        _stack->prev = NULL;
    }
    else
    {
        while (s != NULL)
        {
            t = s;
            s = s->next;
        }
        s = temp;
        s->prev = t;
        s->prev->next = s;
    }
}
cardStackList *Shape::remove(cardStackList *_stack)
{
    struct cardStackList *temp, *t, *return_card;
    temp = _stack;
    t = temp;
    while (temp != NULL)
    {
        t = temp;
        if (temp->next != NULL)
        {
            temp = temp->next;
        }
        else
        {
            temp = t;
        }
    }
    temp = t;
    return_card = temp->next;
    temp->next = temp->next->next;
    temp->next->prev = t;
    return return_card;
}
cardStackList *Shape::search(cardStackList *_stack, int _value, int _suit)
{
    struct cardStackList *temp;
    temp = _stack;
    while (temp->_value != _value && temp->_suit != _suit)
    {
        if (temp->next != NULL)
        {
            temp = temp->next;
        }
    }
    if (temp != NULL)
    {
        return temp;
    }
    return NULL; //Only reaches this if the _value and _suit does not exist in that specific stack
}
void Shape::display(cardStackList *_stack)
{

    struct cardStackList *_slot[11];
    /*
    slots 7 to 10 are the Ace Slots
    */
    for (int i = 0; i <= 10; i++)
    {
        _slot[i] = stack[i];
    }
    for (int i = 0; i <= 10; i++)
    {
        while (_slot[i] != NULL)
        {

        }
    }
}
void Shape::CreateStacks()
{
    for (int i = 0; i < 7; i++)
    {
        insert(stack[0]);
    }
    for (int i = 0; i < 6; i++)
    {
        insert(stack[1]);
    }
    for (int i = 0; i < 5; i++)
    {
        insert(stack[2]);
    }
    for (int i = 0; i < 4; i++)
    {
        insert(stack[3]);
    }
    for (int i = 0; i < 3; i++)
    {
        insert(stack[4]);
    }
    for (int i = 0; i < 2; i++)
    {
        insert(stack[5]);
    }
    for (int i = 0; i < 1; i++)
    {
        insert(stack[6]);
    }
    //Stuff that creates the 7 stacks here
}

1 个答案:

答案 0 :(得分:0)

问题出在这一行:

_rCardIter = _cardMatrix.find(i);

_cardMatrix是一张地图,但i是地图中元素的索引(计数)。查找最终将无法找到带有第i个索引的卡并返回end迭代器。

作为一个例子,我假设您的矩阵以卡号0..51开头。删除的第一张牌是第23张牌,因此矩阵有51张牌0..22和24..51。下一次,你再次想要第23张牌。 find找不到,因为该卡已被删除,而您将获得的迭代器将为_cardMatrix.end(),无法取消引用。