字符串数据成员的指针错误 - 获取访问冲突
Bad pointer for a string data member - Getting access violations
编辑: 已解决: 我只是没有初始化currLevel,这导致了数据成员上的错误指针。
我正在使用 SDL 编写一个简单的游戏。 游戏以解决迷宫为中心。 每个迷宫都有一个相应的文本文件,我的程序会读取该文件并相应地设置一个 MazeMap 对象。
我单独测试了它,它似乎初始化正常。但是,当我创建引擎类并在其中创建 MazeMap 对象时,我遇到了此访问冲突,并且 Maze 的名称在调试器中被标记为错误的指针。 这是代码:
迷宫地图.h:
class MazeMap{
public:
MazeMap() {}
~MazeMap();
/*Initializes all the data members using a text file of the following format:
1 |-First line is the level number of the maze
level.png |-Background image for the level
Level Name |-Name of the level
4x4 |-Number of rows x cols
S.XX |-The actual map:
X... | -S: start location
XXX. | -X: Wall, .: passable ground
E... | -E: end of the level*/
void init(std::string level_file);
//Prints the maze to std::cout
void print() const;
//Calls uti::apply_surface() on all the surfaces to prepare them for blitting
// Surfaces are created for each tile.
// Will be called in Engine::render()
void drawMaze(SDL_Surface *screen) const;
private:
std::string MazeName; //THE CULPRIT!
int level,
rows,
cols;
std::vector< std::vector<Tile> > tiles;
SDL_Surface* background;
//Used in init() to get level, rows, cols, and MazeName,
// as well as initialize the background image.
void initMapInfo(std::fstream& map_in);
//Used in init() to convert the characters in the text file
// to tiles for the tile vector.
Tile convert_char_to_tile(char t) const;
//Used in print() to convert the tiles back to chars for
// printing.
char convert_tile_to_char(Tile t) const;
};
发生运行时错误的 initMapInfo 函数:
void MazeMap::initMapInfo(std::fstream& map_in){
char x;
std::string holder;
//First line: get level number
std::getline(map_in, holder);
level = uti::string_to_int(holder);
//Second line: get background image file name
std::getline(map_in, holder);
background = uti::load_image(holder);
//Third line: get name of the level
std::getline(map_in, MazeName); //THIS LINE IS FAILING
//Fourth line: get rows and cols
std::getline(map_in, holder);
std::stringstream s(holder);
s >> rows >> x >> cols;
}
发动机类:
class Engine{
public:
Engine();
void run();
private:
SDL_Surface *screen;
GameState currentState;
int currLevel;
MazeMap levels[NUM_LEVELS];
Player player;
//Initializes the screen and sets the caption.
void initSDL();
/******************************************************/
/* Primary functions to be used in the main game loop */
//First, input from the player will be taken
void processInput();
//Based on the user input, various game world elements will be updated.
void update();
//Based on what was updated, the screen will be redrawn accordingly.
void render();
/////////////////////////////////////////////////////////
};
run() 函数:
void Engine::run(){
bool play = true;
MazeMap *curr = &levels[currLevel];
curr->init(TEST_MAZE);
curr->drawMaze(screen);
while(play){
}
}
任何帮助,不胜感激。 我意识到这里有很多代码,为此我深表歉意。 我只是想彻底。 谢谢。
@Slims自己找到了原因:
解决:我只是没有初始化
currLevel
,这导致了数据成员上的错误指针?
相关文章:
- 写入位置0x0000000C时发生访问冲突
- 引发异常:读取访问冲突**dynamicArray**为0x1118235.发生
- 链表中写入访问冲突的未知原因
- C++中的openCV Mat访问冲突
- C++尝试深度复制唯一指针时出现内存访问冲突
- C++ 中动态二维数组的访问冲突
- 使用 ReadProcessMemory 获取字符串值的访问冲突
- 调用基构造函数时遇到问题 - 获取读取访问冲突
- 在Visual Studio Community中使用glfw3 glew和opengl获取访问冲突异常
- 获取访问冲突读取位置0x000000b0与 glutMouseFunc
- MFC:在工作线程中使用对象的成员函数时获取访问冲突错误
- 获取IDropTarget与HWND抛出0xC0000005访问冲突
- 将两个字符串一起分配,获取访问读取冲突
- 使用 GetFileVersionInformation 获取访问冲突错误
- 字符串数据成员的指针错误 - 获取访问冲突
- 试图从地址中获取值会导致访问冲突
- 获取字符串后,CompStr(BSTR,BSTR)上发生访问冲突
- 当试图获取系统时间时,访问冲突读取位置
- 在代码的不同部分中获取访问冲突错误
- 从二进制文件获取输入时发生访问冲突错误