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



我似乎找不到我的问题在哪里。这是一个三文件程序,一个文件中有一个Die类,另一个文件里有一个Histogram类,还有main.cpp文件。它应该打印一个用X构造的直方图,以显示模具在六个面中的每一个面上落下的次数。由于矢量错误,我无法前进。。。这个程序可能还有其他问题,我还没有解决,但我只想知道矢量误差。非常感谢。

main.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:

#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

a直方图.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

我没有找到初始化直方图的位置,这可能是问题所在。但即使你修复了它,你也会遇到另外两个错误:

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

您正在访问元素CCD_ 1,而它可能应该是CCD_。在你有的线路上也有同样的问题

largeBin = binHistogram.at(i);

这很可能是导致错误的一行(上面的一行不会很好地告诉你问题出在哪里,只是让你的程序崩溃)。

您永远不会更改aHistogram类中向量的大小,这意味着它的大小将始终为零。任何索引都将超出范围。

对于直方图之类的东西,我实际上建议您使用std::unorderd_map而不是std::vector,其中"face"是关键,count是数据。然后你可以做例如

binHistogramMap[face] += 1;

而不用担心CCD_ 6的元素不存在(如果条目不存在,它将被创建并初始化为零)。

最新更新