[英]C++ vector ERROR: subscript out of range
我是C ++新手,目前正在嘗試為學位創建基於文本的迷宮游戲。 我正在用向量(用於動態大小)替換當前正在工作的數組,並向其中讀取chars的txt文件。
問題:我沒有得到IDE錯誤,但是卻得到運行時錯誤:調試斷言失敗! 表達式:向量下標超出范圍!
我使用了推后而不是RoomFile = data,但是我不知道是否需要修改其他功能。 我在另一個類(播放器)中讀取了數組/矢量數據,並將在需要時包含代碼。
我到處搜索以找到解決方案,但找不到使用類似矢量結構的任何項目。 我究竟做錯了什么?
#pragma once
#include <vector>
#include "stdafx.h"
class Room
{
private: //data acceessed by functions
char floor;
char wall;
char door;
int width;
int height;
public:
std::vector <char> data;
Room* North;
Room* East;
Room* South;
Room* West;
Room* Next;
int NorthX;
int NorthY;
int EastX;
int EastY;
int SouthX;
int SouthY;
int WestX;
int WestY;
char RoomFile;
//where functions go
Room(void);
void CreateRoom(int RoomNo);
void PrintRoom();
~Room(void);
};
#include <iostream>
#include <fstream>
#include <streambuf>
#include <string>
#include <sstream>
#include "Room.h"
#include <cstdlib>
using namespace std;
Room::Room(void)
{
floor = ' ';
wall = '#';
door = 'X';
width = 11;
height = 11;
North=0;
East=0;
South=0;
West=0;
Next=0;
NorthX=0;
NorthY=0;
EastX = 0;
EastY = 0;
SouthX= 0;
SouthY=0;
WestX=0;
WestY=0;
}
void Room::CreateRoom(int RoomNo)
{
std::vector<char> data;
char RoomFile[255];
ifstream infile;
stringstream ss;
//LOAD CURRENT ROOM FROM FILE
ss << RoomNo;
string str = ss.str();
string fname = ("Room");
fname.append(str);
fname.append(".txt");
infile.open(fname);
infile.clear();
infile.seekg(0);
if(infile.is_open())
{
while(!infile.eof()) // To get you all the lines.
{
int i;
for(int row = 0; row < width; row++)
{
infile.getline(RoomFile, 256);
i = 0;
for(int col = 0; col < height; col++)
{
data.push_back(RoomFile[i]);
i++;
}
}
}
}
else
{
cout << "ERROR: infile not open" << endl;
}
infile.close();
//ASSIGN DOORS TO ROOMS IF NEEDED
// treating the array as being one-dimensional.
for(int row = 0; row < width; row++)
{
for(int col = 0; col < height; col++)
{
if(North!=NULL)
{
if(data[row*height+col]=='X')
{
//data[row*height+col] = door;
NorthX=row;
NorthY=col;
}
}
if(East!=NULL)
{
if(data[row*height+col]=='X')
{
//data[row*height+col] = door;
EastX = row;
EastY = col;
}
}
if(South!=NULL)
{
if(data[row*height+col]=='X')
{
//data[row*height+col] = door;
SouthX = row;
SouthY = col;
}
}
if(West!=NULL)
{
if(data[row*height+col]=='X')
{
//data[row*height+col] = door;
WestX = row;
WestY = col;
}
}
}
}
}
void Room::PrintRoom()
{
for(int row = 0; row < width; row++)
{
for(int col = 0; col < height; col++)
{
cout << data[col * height + row];
}
cout << "\n";
}
cout << endl;
}
Room::~Room(void)
{
}
// throw -- terminate on thrown exception REPLACEABLE
#define _HAS_EXCEPTIONS 0
#include <cstdio>
#include <cstdlib>
#include <exception>
#include <crtdbg.h>
_STD_BEGIN
#ifdef _DEBUG
_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL _Debug_message(const wchar_t *message, const wchar_t *file, unsigned int line)
{ // report error and die
if(::_CrtDbgReportW(_CRT_ASSERT, file, line, NULL, L"%s", message)==1)
{
::_CrtDbgBreak();
}
}
_CRTIMP2_PURE void __CLRCALL_PURE_OR_CDECL _Debug_message(const unsigned short *message, const unsigned short *file, unsigned int line)
{ // report error and die
_Debug_message((wchar_t *) message, (wchar_t *) file, line);
}
#endif
_STD_END
我認為這是錯誤:你在打印PrintRoom
是std::vector<char> data
類里面,而std::vector<char> data
,你填補了是本地CreateRoom
方法。
void Room::CreateRoom(int RoomNo)
{
std::vector<char> data; // remove this to solve
在您的PrintRoom()函數中,
cout << data[col * height + row];
代替
cout << data[row * height + col];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.