C++ 邻接矩阵设置链接不正确
我正在尝试为基于文本的“3-D迷宫”(实现为7x7矩阵)实现一个邻接矩阵。出于某种原因,我目前的代码使玩家总是可以选择向下移动,这是不应该发生的。我的测试行表明文件输入是正确的,并且pIdx、idx和dCode参数正在使用正确的值将其发送到第二个函数 问题就在第二个函数的最后一行吗C++ 邻接矩阵设置链接不正确,c++,adjacency-matrix,C++,Adjacency Matrix,我正在尝试为基于文本的“3-D迷宫”(实现为7x7矩阵)实现一个邻接矩阵。出于某种原因,我目前的代码使玩家总是可以选择向下移动,这是不应该发生的。我的测试行表明文件输入是正确的,并且pIdx、idx和dCode参数正在使用正确的值将其发送到第二个函数 问题就在第二个函数的最后一行吗 { //This function reads in the directions from a file. If it reads in -1, there should not be a
{
//This function reads in the directions from a file. If it reads in -1, there should not be a
//link to that direction.
// Read North
getNextLine(line, 128);
link = atoi(line);
setLink(i, link, 'N');
// Read South
getNextLine(line, 128);
link = atoi(line);
setLink(i, link, 'S');
// Read East
getNextLine(line, 128);
link = atoi(line);
setLink(i, link, 'E');
// Read West
getNextLine(line, 128);
link = atoi(line);
setLink(i, link, 'W');
// Read Up
getNextLine(line, 128);
link = atoi(line);
setLink(i, link, 'U');
// Read Down
getNextLine(line, 128);
link = atoi(line);
setLink(i, link, 'D');
}
此功能的问题在于:
void Map::setLink(int pIdx, int linkIdx, char dCode)
{
if ((pIdx<0)||(pIdx>6))
{
cout<<"pIdx invalid"<<endl;
exit(-1);
}
else if ((linkIdx<-1)||(linkIdx>6))
{
cout<<"LinkIdx invalid"<<endl;
exit(-1);
}
else if ((dCode!='N')&& (dCode!='S')&& (dCode!='E')&& (dCode!='W')&&(dCode!='U')&&(dCode!='D'))
{
cout<<"dCode must equal 'N', 'S','E', 'W', 'U', or 'D'."<<endl;
exit(-1);
}
m_cAdjMatrix[pIdx][linkIdx]=dCode;
}
void Map::setLink(int-pIdx、int-LinkId、char-dCode)
{
if((pIdx6))
{
cout当linkIdx
为-1时,setLink
末尾的赋值将访问数组的边界之外(m_cAdjMatrix[pIdx][1]
),从而导致未定义的行为
如果linkIdx
为-1,则不希望执行该赋值