C ++向量错误:下标超出范围

时间:2013-03-16 12:23:43

标签: c++ vector runtime-error subscript

我是一名C ++菜鸟,目前正试图为学位创建基于文本的迷宫游戏。我用一个向量(动态大小)替换我当前正在工作的数组,并读取一个字符的txt文件。

问题:我没有IDE错误,但得到RUNTIME ERROR:调试断言失败!表达式:向量下标超出范围!

我使用了pushback而不是RoomFile = data,但我不知道是否需要修改其他功能。我在另一个类(播放器)中读​​取数组/矢量数据,并在需要时包含代码。

我到处寻找解决方案,但找不到任何使用类似矢量结构的项目。我做错了什么?

Room.h

#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);

};

Room.cpp

#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

2 个答案:

答案 0 :(得分:2)

我认为这是错误:您在PrintRoom打印的内容是您班级中的std::vector<char> data,而您填写的std::vector<char> dataCreateRoom的本地内容方法

void Room::CreateRoom(int RoomNo)
{
std::vector<char> data; // remove this to solve

答案 1 :(得分:1)

在您的PrintRoom()函数中,您有

cout << data[col * height + row];

而不是

cout << data[row * height + col];