【问题标题】:C++ vector ERROR: subscript out of rangeC++ 向量错误:下标超出范围
【发布时间】:2017-01-30 22:46:10
【问题描述】:

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

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

我使用了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

【问题讨论】:

  • 嗯,这样断言就会说明它失败的地方。这是有用的信息。
  • 为什么他们不能在 Unis 正确教授 C++?
  • 程序:C:\Windows\system32\MSVCP110D.dll 文件:d:\program files (x86)\microsoft visual studio 11.0\vc\include\vector 行:1140
  • 这个断言反馈可以帮助找到什么问题?
  • 你需要一个堆栈跟踪。

标签: c++ vector runtime-error subscript


【解决方案1】:

我认为这是错误:您在 PrintRoom 中打印的是类中的 std::vector&lt;char&gt; data,而您填写的 std::vector&lt;char&gt; data 是 CreateRoom 方法的本地内容。

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

【讨论】:

  • 体面的编译器会对此发出警告。
【解决方案2】:

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

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

而不是

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

【讨论】:

  • 为找到已修改的问题而欢呼,但仍然出现运行时错误!
猜你喜欢
  • 2011-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多