首先對不起我的英文不好,希望你們能理解我:)編寫WinAPI遊戲和我的類的行爲非常奇怪:所有操作與向量 崩潰我的程序,所以Windows說我的.exe停止工作。但是,當我調試這些行 我收到異常。C++ std ::向量迭代器錯誤
這是我的類的頭看起來像:
#ifndef FIGURE_H_INCLUDED
#define FIGURE_H_INCLUDED
#include <vector>
#include <Windows.h>
#include "Other.h"
using namespace std;
enum Figure_Type { I, J, L, O, S, T, Z };
class Figure
{
public:
/* CONSTRUCTORS */
Figure();
Figure(Figure_Type);
/* MOVEMENT */
bool Move(vector<Cell>&, Direction&);
void Drop(vector<Cell>&);
bool Rotate(vector<Cell>&);
/* OTHER */
void Draw(HDC&);
private:
/* METHODS */
void Generate();
void GenerateMasks();
void GenerateFigure();
Figure GetFigureCopy() const;
/* DATA */
Shift shift;
char mask[4][4];
vector<Cell> vCells;
Figure_Type type;
int rotation;
};
#endif
我的構造函數使用生成()方法,它的代碼是:
void Figure::GenerateFigure()
{
vCells.clear();
int defPosX = 4,
defPosY = 20;
Cell cell;
for(int y = 0; y < 4; y++)
{
for(int x = 0; x < 4; x++)
{
if(mask[y][x] == '0')
{
cell.x = defPosX + x + shift.dx;
cell.y = defPosY - y + shift.dy;
vCells.push_back(cell);
}
}
}
}
而且我對vCells.clear越來越例外()方法和(如果我評論第一行)vCells.push_back(cell)行。實際上,每次使用矢量/矢量迭代器的操作都會使我的程序崩潰,甚至會增加迭代器,這些只是第一次,所以我的代碼不再運行。 異常文本:
「未處理的異常在0x5A4ACCD2(msvcp110d.dll)在Tetris_completely_new.exe:0xC000041D:用戶回調期間遇到未處理的異常」
而這些例外情況引發217的「xutility」行。我的評論是:
....
// MEMBER FUNCTIONS FOR _Container_base12
inline void _Container_base12::_Orphan_all()
{ // orphan all iterators
#if _ITERATOR_DEBUG_LEVEL == 2
if (_Myproxy != 0)
{ // proxy allocated, drain it
_Lockit _Lock(_LOCK_DEBUG);
for (_Iterator_base12 **_Pnext = &_Myproxy->_Myfirstiter;
*_Pnext != 0; *_Pnext = (*_Pnext)->_Mynextiter)
**(*_Pnext)->_Myproxy = 0;** // <------------ THIS LINE
_Myproxy->_Myfirstiter = 0;
}
#endif /* _ITERATOR_DEBUG_LEVEL == 2 */
}
....
這裏是我的細胞結構的樣子:
struct Cell
{
Cell() : x(1), y(1) { }
Cell(int _x, int _y): x(_x), y(_y) { }
void Draw(HDC&) const;
bool operator ==(const Cell& a) const { return (x == a.x && y == a.y); }
bool operator !=(const Cell& a) const { return !(*this == a); }
int x;
int y;
};
而且圖構造:
Figure::Figure()
{
srand(time(NULL));
vCells.clear();
type = Figure_Type(rand() % 7);
rotation = 0;
shift.dx = 0;
shift.dy = 0;
Generate();
}
我想看看Cell類的內存分配/釋放。 – user2672165
可以請你展示一段可以用於再現你的問題的代碼 – AnatolyS
你的當前代碼看起來不錯,錯誤是其他地方 – billz