好的,大家好!我又有一個問題..我得到一個未處理的異常。SFML 2.0從地圖文件中加載
我跟蹤這個問題回到它的來源是:
openfile >> MapFile[loadCounterX][loadCounterY];
唯一的例外是:
Unhandled exception at 0x76ee15de in The Vanity Engine.exe: 0xC0000005: Access violation writing location 0x336e880c.
它說,這是訪問的衝突呢我訪問的事情是成功地打開這裏:
std::ifstream openfile(filename);
整個功能是:
//Load map
void Map::Load(const char *filename)
{
//Open the file
std::ifstream openfile(filename);
//Check if file is open
if(openfile.is_open())
{
//Get mapSizeX and Y from the file
openfile >> mapSizeX >> mapSizeY;
//While not at the end of the file
while(!openfile.eof())
{
//Store number at loadCounterX and loadCounterY
openfile >> MapFile[loadCounterX][loadCounterY]; //Error here
//Increment loadCounterX++
loadCounterX++;
//If loadCounterX is less than mapSizeX
if(loadCounterX >= mapSizeX)
{
//Set loadCounterX to 0
loadCounterX = 0;
//Increment loadCounterY
loadCounterY++;
}
}
}
}
映射文件是在Map.H
#ifndef MAP_H
#define MAP_H
#include "SFML\Graphics.hpp"
#include "Global.h"
#include <iostream>
#include <fstream>
class Map
{
public:
void Load(const char *filename);
void Draw(sf::RenderWindow &Window);
private:
int loadCounterX, loadCounterY;
int mapSizeX, mapSizeY;
int MapFile[100][100];
};
#endif
什麼是MapFile,它在哪裏/如何初始化? –
使用Map.H更新的OP – user1816388
mapSizeX和mapSizeY總是<100? – billz