迷宫构造函数问题 [线程 1:EXC_BAD_ACCESS(代码 = 1,地址 = 0x8)]
Maze Constructor Issues [Thread 1: EXC_BAD_ACCESS (code=1, address=0x8)]
当我运行我的程序时,所有内容都会编译,直到Maze
类被实例化。我收到一条警告,说我的Code will never be executed
.cpp编译器(Xcode)将我重定向到The LLVM Compiler Infrastructure
,line 1590
上显示Thread 1: EXC_BAD_ACCESS (code=1, address=0x8)
。指示的代码块是:
template <class _Tp, class _Allocator>
inline _LIBCPP_INLINE_VISIBILITY
void
vector<_Tp, _Allocator>::push_back(const_reference __x)
{
if (this->__end_ != this->__end_cap()) //Thread 1: EXC_BAD_ACCESS (code=1, address=0x8)
{
__RAII_IncreaseAnnotator __annotator(*this);
__alloc_traits::construct(this->__alloc(),
_VSTD::__to_raw_pointer(this->__end_), __x);
__annotator.__done();
++this->__end_;
}
else
__push_back_slow_path(__x);
}
主.cpp:
#include "Maze.h"
#include <iostream>
#include <string>
int main(int argc, char* argv[])
{
std::cout << "-------------tMazet-------------nn";
Maze maze(10);
std::string direction;
while(!true)
{
//Code will never be executed warning HERE
std::cout << "Input a character to move your avatar in the maze and press RETURNn";
std::cout << "(w = UP, s = DOWN, a = LEFT, d = RIGHT)n";
std::cin >> direction;
maze.takeTurn(direction);
maze.printMe();
}
return 0;
}
我的头文件:
#ifndef MAZE_H
#define MAZE_H
#include <vector>
#include <string>
extern const std::string Up;
extern const std::string Down;
extern const std::string Left;
extern const std::string Right;
class Maze
{
private:
char avatarToken; //character that represents the avatar
char emptyToken; //character that represents empty space in the maze
char wallToken; //character that represents a wall
char exitToken; //character that represents an exit - where you quit the game
char stairsUpToken; //character that represents stairs that take you to an UPPER level
char stairsDownToken; //character that represents a Hole that takes you a LOWER level
char bossToken; //character that represents a boss that kills you (you lose/exit the game)
int dim; //width of this square maze
std::vector< std::vector<char> > grid; //a vector of vectors of characters
int avatarRow; //row position of our avatar from zero to dim-1 (NOT from 1 to dim)
int avatarCol; //column position of our avatar from zero to dim-1 (NOT from 1 to dim)
public:
//constructor
Maze(int newDim = 4,
char _avatarToken = 'A',
char _emptyToken = ' ',
char _wallToken = '*',
char _exitToken = 'E',
char _stairsUpToken = '^',
char _stairsDownToken = 'V',
char _bossToken = 'B');
void printMe();
bool takeTurn(std::string direction); //move the avatar in the requested direction if possible
};
#endif
我的实现文件:
#include "Maze.h"
#include "Keyboard.h" //contains global variables: Up, Down, Left, Right
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <ctime>
//1st constructor
Maze::Maze(int newDim,
char _avatarToken,
char _emptyToken,
char _wallToken,
char _exitToken,
char _stairsUpToken,
char _stairsDownToken,
char _bossToken) : dim(newDim),
avatarToken(_avatarToken),
emptyToken(_emptyToken),
wallToken(_wallToken),
exitToken(_exitToken),
stairsUpToken(_stairsUpToken),
stairsDownToken(_stairsDownToken),
bossToken(_bossToken)
{
//place avatar location in center of maze
this->avatarRow = newDim / 2;
this->avatarCol = newDim / 2;
//initialize top wall of grid
for(int i = 0; i < this->dim; i++)
this->grid[0].push_back(this->wallToken);
//initialize middle of grid
for(int j = 1; j < this->dim - 1; j++)
for(std::vector<char>::iterator it = this->grid[j].begin(); it < this->grid[j].end(); it++)
it == this->grid[j].begin() ||
it == this->grid[j].end() ? this->grid[j].push_back(this->wallToken) :
this->grid[j].push_back(this->emptyToken);
//initialize bottom wall of grid
for(int l = 0; l < this->dim; l++)
this->grid[dim - 1].push_back(this->wallToken);
//place exit token in random spot in the maze
srand( (unsigned int) time(0)); //random seed for exit token placement in maze
int rand_wall = rand() % 4 + 1; //determine which wall exit token is on
int rand_placement = rand() % this->dim + 1; //placement of exit on wall
switch(rand_wall)
{
case 1:
this->grid[0][rand_placement] = this->exitToken;
break;
case 2:
this->grid[rand_placement][0] = this->exitToken;
break;
case 3:
this->grid[this->dim - 1][rand_placement] = this->exitToken;
break;
case 4:
this->grid[rand_placement][this->dim - 1] = this->exitToken;
break;
}
//place stairs up token in random spot in the maze
srand( (unsigned int) time(0)); //random seed for stairs up token placement in maze
rand_wall = rand() % 4 + 1; //determine which wall stairs up token is on
rand_placement = rand() % this->dim + 1; //placement of stairs up on wall
switch(rand_wall)
{
case 1:
//check to see if element is occupied by exit token
if(this->grid[0][rand_placement] != this->exitToken)
this->grid[0][rand_placement] = this->stairsUpToken;
break;
case 2:
//check to see if element is occupied by exit token
if(this->grid[rand_placement][0] != this->exitToken)
this->grid[rand_placement][0] = this->stairsUpToken;
break;
case 3:
//check to see if element is occupied by exit token
if(this->grid[this->dim - 1][rand_placement] != this->exitToken)
this->grid[this->dim - 1][rand_placement] = this->stairsUpToken;
break;
case 4:
//check to see if element is occupied by exit token
if(this->grid[rand_placement][this->dim - 1] != this->exitToken)
this->grid[rand_placement][this->dim - 1] = this->stairsUpToken;
break;
}
//place stairs down token in random spot in the maze
srand( (unsigned int) time(0)); //random seed for stairs down token placement in maze
rand_wall = rand() % 4 + 1; //determine which wall stairs down token is on
rand_placement = rand() % this->dim; //placement of stairs down on wall
switch(rand_wall)
{
case 1:
//check to see if element is occupied by exit token or stairs up token
if(this->grid[0][rand_placement] != this->exitToken ||
this->grid[0][rand_placement] != this->stairsUpToken)
{
this->grid[0][rand_placement] = this->stairsDownToken;
}
break;
case 2:
//check to see if element is occupied by exit token or stairs up token
if(this->grid[rand_placement][0] != exitToken ||
this->grid[rand_placement][0] != this->stairsUpToken)
{
this->grid[rand_placement][0] = this->stairsDownToken;
}
break;
case 3:
//check to see if element is occupied by exit token or stairs up token
if(this->grid[this->dim - 1][rand_placement] != this->exitToken ||
this->grid[this->dim - 1][rand_placement] != this->stairsUpToken)
{
this->grid[this->dim - 1][rand_placement] = this->stairsDownToken;
}
break;
case 4:
//check to see if element is occupied by exit token or stairs up token
if(this->grid[rand_placement][this->dim - 1] != this->exitToken ||
this->grid[rand_placement][this->dim - 1] != this->stairsUpToken)
{
this->grid[rand_placement][this->dim - 1] = this->stairsDownToken;
}
break;
}
//place boss token in random spot inside the maze
srand( (unsigned int) time(0)); //random seed for boss token placement in maze
rand_placement = rand() % this->dim; //placement of boss token
switch(rand_wall)
{
case 1:
//check to see if element is occupied by exit token or stairs up token
if(this->grid[0][rand_placement] != this->exitToken ||
this->grid[0][rand_placement] != this->stairsUpToken)
{
this->grid[0][rand_placement] = this->stairsDownToken;
}
break;
case 2:
//check to see if element is occupied by exit token or stairs up token
if(this->grid[rand_placement][0] != exitToken ||
this->grid[rand_placement][0] != this->stairsUpToken)
{
this->grid[rand_placement][0] = this->stairsDownToken;
}
break;
case 3:
//check to see if element is occupied by exit token or stairs up token
if(this->grid[this->dim - 1][rand_placement] != this->exitToken ||
this->grid[this->dim - 1][rand_placement] != this->stairsUpToken)
{
this->grid[this->dim - 1][rand_placement] = this->stairsDownToken;
}
break;
case 4:
//check to see if element is occupied by exit token or stairs up token
if(this->grid[rand_placement][this->dim - 1] != this->exitToken ||
this->grid[rand_placement][this->dim - 1] != this->stairsUpToken)
{
this->grid[rand_placement][this->dim - 1] = this->stairsDownToken;
}
break;
}
}
//print maze row by row
void Maze::printMe()
{
//rows
for(int m = 0; m < this->dim; m++)
{
//columns
for(int n = 0; n < this->dim; n++)
{
//print avatar token
if(m == this->avatarRow && n == this->avatarCol)
std::cout << this->avatarToken;
else
std::cout << this->grid[m][n];
}
std::cout << std::endl;
}
}
//update location of the avatar
bool Maze::takeTurn(std::string _direction)
{
if(_direction == Up && this->grid[this->avatarRow - 1][this->avatarCol] != this->wallToken)
{
--this->avatarRow;
if(this->grid[this->avatarRow - 1][this->avatarCol] == this->exitToken ||
this->grid[this->avatarRow - 1][this->avatarCol] == this->bossToken ||
this->grid[this->avatarRow - 1][this->avatarCol] == this->stairsUpToken ||
this->grid[this->avatarRow - 1][this->avatarCol] == this->stairsDownToken)
{
std::cout << "You exited the maze.n";
return true;
}
else return false;
}
else if(_direction == Down && this->grid[this->avatarRow + 1][this->avatarCol] != this->wallToken)
{
++this->avatarRow;
if(this->grid[this->avatarRow + 1][this->avatarCol] == this->exitToken ||
this->grid[this->avatarRow + 1][this->avatarCol] == this->bossToken ||
this->grid[this->avatarRow + 1][this->avatarCol] == this->stairsUpToken ||
this->grid[this->avatarRow + 1][this->avatarCol] == this->stairsDownToken)
{
std::cout << "You exited the maze.n";
return true;
}
else return false;
}
else if(_direction == Left && this->grid[this->avatarRow][this->avatarCol - 1] != this->wallToken)
{
--this->avatarCol;
if(this->grid[this->avatarRow][this->avatarCol - 1] != this->exitToken ||
this->grid[this->avatarRow][this->avatarCol - 1] != this->bossToken ||
this->grid[this->avatarRow][this->avatarCol - 1] != this->stairsUpToken ||
this->grid[this->avatarRow][this->avatarCol - 1] != this->stairsDownToken)
{
std::cout << "You exited the maze.n";
return true;
}
else return false;
}
else if(_direction == Right && this->grid[this->avatarRow][this->avatarCol + 1] != this->wallToken)
{
++this->avatarCol;
if(this->grid[this->avatarRow][this->avatarCol + 1] != this->exitToken ||
this->grid[this->avatarRow][this->avatarCol + 1] != this->bossToken ||
this->grid[this->avatarRow][this->avatarCol + 1] != this->stairsUpToken ||
this->grid[this->avatarRow][this->avatarCol + 1] != this->stairsDownToken)
{
std::cout << "You exited the maze.n";
return true;
}
return false;
}
else return false;
}
编辑:从class Maze
中删除了不必要的类成员。
this->grid[0].push_back(this->wallToken);
在为grid
分配任何存储空间之前将项目推送到grid[0]
。没有grid[0]
可以安全地调用push_back
。
可能的解决方案:
预分配成员初始值设定项中的所有内容。
Maze::Maze(int newDim,
char _avatarToken,
char _emptyToken,
char _wallToken,
char _exitToken,
char _stairsUpToken,
char _stairsDownToken,
char _bossToken) : avatarToken(_avatarToken),
emptyToken(_emptyToken),
wallToken(_wallToken),
exitToken(_exitToken),
stairsUpToken(_stairsUpToken),
stairsDownToken(_stairsDownToken),
bossToken(_bossToken),
dim(newDim), // moved
grid(dim, std::vector<char>(dim)) //added
然后使用运算符[]
而不是将角色推入迷宫。例如
for(int i = 0; i < this->dim; i++)
this->grid[0][i] = this->wallToken;
再想一想,
grid(dim, std::vector<char>(dim, _wallToken))
通过将网格中的每个空间初始化为墙,将消除用于设置边界墙的循环的需要。为您节省一些工作。
还要关注
int rand_placement = rand() % this->dim + 1;
rand_placement
可以等于dim
,vector
s 仅对 0 到 dim-1 有效。
this->grid[rand_placement][0] = this->exitToken;
和其他人写出界外。
相关文章:
- 为什么我得到以下代码地址清理器:未知地址错误的SEGV
- 我有一个线程 1:EXC_BAD_ACCESS(代码 = 1,地址 = 0x8)错误.我认为这是由于内存管理不好.我可以
- 为什么下面的代码段返回指针指向的值而不是指针的地址?
- 使用双LL在C++中实现Stack失败,出现异常"EXC_BAD_ACCESS(代码=2,地址=0x7fff5
- 使用全局 IP 地址时,C++ winsock 2 应用程序中的代码是否必须更改?
- 线程 1:xcode 中出现EXC_BAD_ACCESS(代码 = 1,地址 = 0x0)错误
- Xcode:线程1:exc_bad_access(代码= 1,地址= 0x0)在缩短列表时
- 为什么这段代码给出错误......'a'是指针到字符指针,应该存储's'的地址,因为s是指向数组的第一个元素的指针
- C++ - 使用代码块从文件中读取 IP 地址
- exc_bad_access(代码= 1,地址= 0x68)在第二个读取方法中
- OPENCL EXC_BAD_ACCESS(代码= 1,地址= 0xc00)
- 线程 1:EXC_BAD_ACCESS(代码 = 1,地址 = 0x0)错误
- 如何调试错误代码 77:cudaErrorIllegal地址当 cuda-memcheck 没有发现问题时
- 无法找出此错误(将结构数组传递给函数):线程 1:EXC_BAD_ACCESS(代码 = 1,地址 = 0x1)
- 为什么此代码打印字符串而不是打印地址
- JIT代码生成后,如何检索LLVM ::指令地址
- 迷宫构造函数问题 [线程 1:EXC_BAD_ACCESS(代码 = 1,地址 = 0x8)]
- 线程 1:EXC_BAD_ACCESS(代码 = 1,地址 = 0x8)
- 如何禁用调试控制台在我的代码中显示文件地址
- 正在Mac上的Xcode上尝试C++-ERROR-exc_bad_access(代码=1地址=0x100500000)