简体   繁体   English

为什么会出现超出范围的错误?

[英]Why am i getting out-of-range error?

I can't seem to find where my issue is.我似乎无法找到我的问题所在。 Its a three file program with aDie class in one file, aHistogram class in another file, and the main.cpp file.它是一个三文件程序,一个文件中有一个Die 类,另一个文件中有一个Histogram 类,以及main.cpp 文件。 It is supposed to print a histogram constructed with X's to show how many times the die landed on each of the six faces.它应该打印一个由 X 构成的直方图,以显示骰子落在六个面中的每一个面上的次数。 I cant move forward because of the vector error... There may be other issues with the program that i haven't worked out yet, but I just want to know about the vector error.由于向量错误,我无法继续前进......程序可能还有其他问题,我还没有解决,但我只想知道向量错误。 Thank you.谢谢你。

main.cpp:主.cpp:

#include <iostream>
#include <stdlib.h> //srand and rand
#include <time.h> //Time
#include <vector>
#include <algorithm>
#include "aHistogram.h"
#include "aDie.h"

using namespace std;

int main()
{
    srand (time(NULL));
    int numRolls;
    const int maxLengthOfLine = 50;

    cout << "How many rolls? " << endl;
    cin >> numRolls;

    aDie fairDie;
    aHistogram fairHistogram;

    //For Loop rolls the die and updates the histogram vector ~~binHistogram.
    for(int i = 0; i < numRolls; i++)
    {
        int face = fairDie.roll();
        fairHistogram.update(face);
    }

    cout << "*******************" << endl;
    cout << "*****Histogram*****" << endl;
    cout << "*******************" << endl;

    fairHistogram.display(maxLengthOfLine);


}

aDie.h: aDie.h:

#ifndef ADIE_H_INCLUDED
#define ADIE_H_INCLUDED

#include <stdlib.h>
#include <time.h>
#include <vector>
#include <algorithm>

using namespace std;

/********************************************/
/*******Definition of aDie class*************/
/********************************************/
class aDie
{
    public:
        int roll(); //return an integer between 1 and 6 to represent what face appears when the die is rolled.
        aDie(); //Default constructor
        ~aDie(); //Destructor

    private:

        int numFaces = 6;
};

int aDie::roll()
{
    return ((rand() % numFaces) + 1); //returns a random number between 1 and 6
}

aDie::aDie()
{
    cout << "Dice Roll...." << endl;
    return;
}

aDie::~aDie()
{
    return;
}

#endif // ADIE_H_INCLUDED

aHistogram.h:直方图.h:

#ifndef AHISTOGRAM_H_INCLUDED
#define AHISTOGRAM_H_INCLUDED

#include <algorithm>
#include <stdlib.h>
#include <vector>

using namespace std;

/********************************************/
/*******Definition of aHistogram class*******/
/********************************************/

class aHistogram
{
    public:
        void update(int face);
        void display(int maxLengthOfLine);
        int Count(int face);
        void clear();

        aHistogram(); //Constructor
        ~aHistogram(); //Destructor

    private:
        vector<int> binHistogram;
        const int numFaces = 6;
        int totalRolls;
        int largeBin = 0;
        double xScale;
};

//Adds a count to each face every time the die lands on said face.
void aHistogram::update(int face)
{
    binHistogram.at(face) += 1;
    return;
}

//Displays the histogram with X's
//maxLengthOfLine represents the maximum number of x’s to be printed for the largest bin count.
void aHistogram::display(int maxLengthOfLine)
{
    xScale = maxLengthOfLine / largeBin;
    for(int i = 1; i <= 6; i++)
    {
        cout << i << " : " << Count(i) << " : ";
        int numXs = xScale * binHistogram.at(i);
        for(int j = 0; j < numXs; j++)
        {
            cout << "X";
        }
    }
}

//To be called AFTER aHistogram::update
//Returns a count of how many times for each face of the die
int aHistogram::Count(int face)
{
    //For Loop determines the largest bin count
    for (int i = 1; i < numFaces; i++)
    {
        while (binHistogram[i] >= largeBin)
        {
            largeBin = binHistogram.at(i);
        }
    }

    //
    return binHistogram.at(face);
}

void aHistogram::clear()
{
    binHistogram.clear();
    return;
}

//Defines the DEFAULT CONSTRUCTOR. Sets all elements of the histogram to zero.
aHistogram::aHistogram()
{
    return;
}

//Defines the DESTRUCTOR. Clears vector after use.
aHistogram::~aHistogram()
{
    binHistogram.clear(); //Clears vector
    return;
}



#endif // AHISTOGRAM_H_INCLUDED

I didnt find the place where you initialize the histogram this might be the problem.我没有找到初始化直方图的地方,这可能是问题所在。 But even if you fix that, you will hit two other bugs:但即使你解决了这个问题,你也会遇到另外两个错误:

for (int i = 1; i < numFaces; i++)
{
    while (binHistogram[i] >= largeBin)
    {
        largeBin = binHistogram.at(i);
    }
}

you are accessing elements 1....6 when probably it should be 0...5 .您正在访问元素1....6时可能应该是0...5 Same problem in the line where you have在您所在的行中遇到同样的问题

largeBin = binHistogram.at(i);

which is most likely the line that causes the error (the one above wont be so nice to tell you what is the problem but just crash your program).这很可能是导致错误的行(上面的行不会很好地告诉您问题是什么,但只会使您的程序崩溃)。

You never change the size of the vector in the aHistogram class, which means its size will always zero.你永远不会改变aHistogram类中向量的大小,这意味着它的大小将始终为零。 Any index will be out of bounds.任何索引都会越界。

For things like histograms I would actually recommend you to use std::unorderd_map instead of std::vector , with the "face" being the key and the count being the data.对于直方图之类的东西,我实际上建议您使用std::unorderd_map而不是std::vector ,“面”是键,计数是数据。 Then you could do eg然后你可以做例如

binHistogramMap[face] += 1;

without worrying about the element for face not existing (it will be created and initialized to zero if the entry doesn't exist).不用担心face元素不存在(如果条目不存在,它将被创建并初始化为零)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我得到这个超出范围的错误 - Why am I getting this out of range error 为什么我得到“矢量迭代器+偏移超出范围”错误? - Why am I getting a “vector iterator + offset out of range” error? 为什么我会收到std :: out_of_range错误? - Why am I getting std::out_of_range error? 为什么会出现“向量超出范围”错误? - Why am I getting a “vector out of range” error? 为什么在合并排序中出现矢量下标超出范围错误? - Why am I getting vector subscript out of range error in the Merge Sort? 尝试使用for循环创建和填充向量时出现超出范围的错误(C ++) - Getting out-of-range error when trying to create and populate a vector using a for loop (C++) 为什么我的“向量下标超出范围”? - Why am I getting 'vector subscript out of range'? 在遍历字符串时,即使我已经超出了长度,为什么还没有超出范围错误? - While iterating through a string, why am I NOT getting out of range error, even though I'm already beyond its length? 使用 c++,为什么我明知不是,却收到错误消息“字符串下标超出范围”? 或者至少看起来是这样 - Using c++, why am I getting the error message "string subscript out of range" when I know it is not? Or at least it seems that way 取消引用一个超出范围的矢量迭代器 - 什么是开心的,为什么? - Dereferencing an out-of-range vector iterator - what happpens and why?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM