类和向量错误:SIGSEGV

时间:2014-11-23 12:31:16

标签: c++ class vector

我正在做一个小游戏,现在我正在尝试编写Characer动作,所以这里是执行错误的代码:

当我致电charac.move(DOWN,terrain);时,代码是

void Character::move(direction d,Terrain& terrain){
    if(d==UP && !terrain.isTileWall(playerCoords.x,playerCoords.y+1)){
        playerCoords.y++;
    }
    if(d==DOWN && !terrain.isTileWall(playerCoords.x,playerCoords.y-1)){ //Here is the error at terrain.isTileWall()
        playerCoords.y--;
    }
    if(d==RIGHT && !terrain.isTileWall(playerCoords.x+1,playerCoords.y)){
        playerCoords.x++;
    }   
    if(d==LEFT && !terrain.isTileWall(playerCoords.x-1,playerCoords.y)){
        playerCoords.x--;
    }
    terrain.setCharacter(this,playerCoords.x,playerCoords.y); //In case this could be a problem Character is a pointer in Terrain code
}

isTileWall()是:

bool Terrain::isTileWall(int x,int y){
    return terrain[x][y].isWall(); //Error is in the [x][y] I think
}

terrain是Terrain类的实例 在这个类中,terrain是一个双向矢量,由terrain = std::vector<std::vector<Tile> >(xSize,std::vector<Tile>(ySize))

声明

isWall只是return bool wall

错误导致矢量标题的这一部分:

  //stl_vector.h

  // element access
  /**
   *  @brief  Subscript access to the data contained in the %vector.
   *  @param __n The index of the element for which data should be
   *  accessed.
   *  @return  Read/write reference to data.
   *
   *  This operator allows for easy, array-style, data access.
   *  Note that data access with this operator is unchecked and
   *  out_of_range lookups are not defined. (For checked lookups
   *  see at().)
   */
  reference
  operator[](size_type __n) _GLIBCXX_NOEXCEPT
  { return *(this->_M_impl._M_start + __n); }

我真的不明白这里有什么问题,所以如果有人理解我会很高兴。

x和y

的任何值都会发生错误

我的程序中有很多代码,所以如果您需要更多代码,请询问

0 个答案:

没有答案