[英]vector subscript out of range :(
So I'm a relatively new c++ programmer.所以我是一个相对较新的 C++ 程序员。 I just made this code as a part of an object-oriented tic-tac-toe I'm working on.我刚刚将此代码作为我正在处理的面向对象的井字游戏的一部分。 It's in Visual Studio Community 2015 and when the code is supposed to check for a winner, I get the error "vector subscript out of range".它在 Visual Studio Community 2015 中,当代码应该检查获胜者时,我收到错误“向量下标超出范围”。 It sucks, it sucks.糟透了,糟透了。
int Board::Win_Check()
{
int ar[8][3] = {
{ 0, 1, 2 },
{ 3, 4, 5 },
{ 6, 7, 8 },
{ 0, 3, 6 },
{ 1, 4, 7 },
{ 2, 5, 8 },
{ 0, 4, 8 },
{ 2, 4, 6 } };
int winner1 = 0;
int winner2 = 0;
bool winner1won = false;
bool winner2won = true;
for (int i = 0; i < 9; ++i)
{
for (int j = 0; j < 3; ++j)
{
if (m_board[ar[i][j]] == 'X')
{
++winner1;
}
else if (m_board[ar[i][j]] == 'Y')
{
++winner2;
}
if (winner1 != 3)
{
winner1 = 0;
}
else
{
winner1won = true;
}
if (winner2 != 3)
{
winner2 = 0;
}
else
{
winner2won = true;
}
}
}
int return_type;
if (winner1won == true)
{
return_type = 1;
}
else if (winner2won == true)
{
return_type = 2;
}
else
{
return_type = 0;
}
return return_type;
}
You are obviously going out of range with your indices here您的索引显然超出了此处的范围
for (int i = 0; i < 9; ++i) {
should be应该
for (int i = 0; i < 8; ++i) {
// ^
as the array was declared当数组被声明时
int ar[8][3] = {
// ^
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.