好的大家好!我又遇到了另一个问题。我遇到了一个未处理的异常。
我将问题追溯到它的源头,即:
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++;
}
}
}
}
MapFile 在 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