-1

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

问题:我没有收到 IDE 错误,但收到 RUNTIME ERROR: Debug assertion Failed!表达式:向量下标超出范围!

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

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

房间.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);

};

房间.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
4

2 回答 2

2

我认为这是错误:您要打印的PrintRoomstd::vector<char> data您的班级内部,而std::vector<char> data您填写的是CreateRoom方法的本地内容。

void Room::CreateRoom(int RoomNo)
{
std::vector<char> data; // remove this to solve
于 2013-03-16T12:40:05.457 回答
1

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

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

代替

cout << data[row * height + col];
于 2013-03-16T12:26:26.937 回答