0
編輯:已解決:我只是沒有初始化currLevel,導致數據成員上的錯誤指針。字符串數據成員的錯誤指針 - 獲取訪問違規
我正在使用SDL編寫一個簡單的遊戲。遊戲以解決迷宮爲中心。每個迷宮都有一個對應的文本文件,我的程序可以讀取並相應地設置一個MazeMap對象。
我測試了它的隔離,它似乎是初始化罰款。但是,當我創建了我的引擎類並在其中創建了我的MazeMap對象時,我得到了此訪問衝突,並且在調試器中將迷宮的名稱標記爲不正確的指針。下面是代碼:
MazeMap.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();
/////////////////////////////////////////////////////////
};
運行()功能:
void Engine::run(){
bool play = true;
MazeMap *curr = &levels[currLevel];
curr->init(TEST_MAZE);
curr->drawMaze(screen);
while(play){
}
}
任何幫助表示讚賞。我意識到這裏有很多代碼,爲此我表示歉意。我只是想徹底。謝謝。
如果你自己解決它,你可以發表一個答案並接受。 – chris 2012-04-28 03:07:15
它不會讓我,因爲我沒有足夠的代表:( – Slims 2012-04-28 03:09:31
+1爲了解決自己 – 2012-04-28 03:38:57