我正在使用 Map 来存储字符的位图坐标,以便在从精灵表中呈现字符时使用 . 坐标图定义为

typedef std::array<double,4> Coordinates;
typedef std::unordered_map<char, Coordinates> CharacterMapping;

我一个接一个地提供坐标,就像这样

CharacterMapping *charMap = new CharacterMapping();
(*charMap)['a'][0] = 0; (*charMap)['a'][1] = 0; (*charMap)['a'][2] = 8; (*charMap)['a'][3] = 8;
(*charMap)['b'][0] = 8; (*charMap)['b'][1] = 0; (*charMap)['b'][2] = 16; (*charMap)['b'][3] = 8;
(*charMap)['c'][0] = 16; (*charMap)['c'][1] = 0; (*charMap)['c'][2] = 24; (*charMap)['c'][3] = 8;
(*charMap)['d'][0] = 24; (*charMap)['d'][1] = 0; (*charMap)['d'][2] = 32; (*charMap)['d'][3] = 8;
// and so on, including numbers and some other symbols...
(*charMap)['.'][0] = 49; (*charMap)['.'][1] = 32; (*charMap)['.'][2] = 55; (*charMap)['.'][3] = 40;
(*charMap)[' '][0] = 57; (*charMap)[' '][1] = 32; (*charMap)[' '][2] = 63; (*charMap)[' '][3] = 40;
(*charMap)[','][0] = 1; (*charMap)[','][1] = 40; (*charMap)[','][2] = 7; (*charMap)[','][3] = 48;

然后坐标提供给我创建的类,(Drawable是一个容器类,带有一个指向一个形状对象的指针,表示一个可绘制的对象,Shape是一个表示图形形状的抽象类,SpriteText是一个形状的子类)

SpriteText* spriteTextShape = new SpriteText(Vector(100,700), // position
            ltrTex, // the bitmap
            *charMap, // the map
            "hello world\nalso you\n1. it is ok, dont worry!\n2. what?"); // some text
Drawable *drw = new Drawable(world, // a container of all objects
            spriteTextShape); // the shape

只要我省略空格字符的坐标或在gdb下运行程序,这样就可以正常工作 . 如果程序只是运行,它会在drawable构造函数行上以-1073741819访问冲突错误代码崩溃 . (通过在控制台中打印每一行来识别,不会进入构造函数本身) .

删除该行,将''替换为任何其他字符(如'')或只是在gdb下运行它似乎解决了它 . 为什么在调用完全独立的类构造函数时,键值的更改会影响或导致访问冲突错误?使用gdb时怎么没出现?

ctors:

SpriteText::SpriteText(const Vector& position, Graphics::Texture* const texture,
                    const CharacterMapping& coordinates, const std::string& text) :
        Shape(position) {
    _charDrawer = new CharSprite(position, Vector(1,1), texture, coordinates, 'c');
    _text_info.line_info_r = NULL;
    setText(text);
}
CharSprite::CharSprite(const Vector& position, const Vector& dimensions,
    Texture* const texture, const CharacterMapping& coordinates,
    const char& character):
    Sprite(position,dimensions,texture){
    setSpriteCoordinates(coordinates);
    setCurrentCharacter(character);
}
Drawable::Drawable(World* world, Shape* shape):
    GameObject(world){
    setWorld(world);
    _shape = shape;
    _drawableFlags = Drawable::Flags::BOUND_BY_WORLD;
}

Windows xp,mingw和g .