我有一个定义如下的枚举,所以期望它返回 0-6 之间的值
enum blocks
{ none = 0, red, green, blue, yellow, white, black };
Grid<blocks> gameGrid; // initialised to none
我正在尝试将枚举用作包含块颜色的数组的偏移量。但是gameGrid[y][x]
返回的数字超出了上述范围,导致分段错误。
fixedColor = colourArray[gameGrid[y][x]];
我检查了 y 和 x 在范围 (0-19) 内,gdb 报告:
(gdb) print y $1 = 19 (gdb) print x $2 = 5 (gdb) print gameGrid[y][x] $3 = (blocks &) @0x8201dc4: 136172580
下面是网格的定义:
template <typename T>
class Grid
{
std::deque<Row<T> > rows;
T defaultValue;
public:
const unsigned width, height;
Grid(unsigned inWidth, unsigned inHeight, T inValue) : defaultValue(inValue), width(inWidth), height(inHeight)
{
Row<T> r(width, inValue);
for (unsigned i = height; i; --i)
rows.push_back(r);
}
Row<T>& operator[](unsigned arg) { return rows[arg]; }
......
至于行:
template <typename T>
class Row
{
public:
std::vector<T> elements;
Row(unsigned inWidth, T inValue) : elements(inWidth, inValue) {};
T& operator[](unsigned arg) { return elements[arg]; }
……
我打电话给:
Game::Game(App& inApp) : theApp(inApp), bgColour(sf::Color::Black), gridWidth(10), gridHeight(20), gameGrid(gridWidth, gridHeight, blocks::none),
......
我很好奇积木类型的 gameGrid 的值如何评估为 136172580。对我所缺少的任何想法将不胜感激,谢谢。