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

时间:2015-11-27 15:23:25

标签: c++ class object vector srand

我似乎无法找到问题所在。它是一个三文件程序,在一个文件中有一个类,在另一个文件中有一个单元类,以及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

aHistogram.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

2 个答案:

答案 0 :(得分:0)

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

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

您正在访问元素1....6,可能应该是0...5。您拥有

的行中的问题相同
largeBin = binHistogram.at(i);

这很可能是导致错误的行(上面那个不太好告诉你问题是什么,但只是让程序崩溃)。

答案 1 :(得分:0)

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

对于像直方图这样的东西我实际上建议你使用std::unorderd_map而不是std::vector,使用&#34; face&#34;作为关键,计数是数据。那你就可以做到。

binHistogramMap[face] += 1;

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