繁体   English   中英

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

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

我似乎无法找到我的问题所在。 它是一个三文件程序,一个文件中有一个Die 类,另一个文件中有一个Histogram 类,以及main.cpp 文件。 它应该打印一个由 X 构成的直方图,以显示骰子落在六个面中的每一个面上的次数。 由于向量错误,我无法继续前进......程序可能还有其他问题,我还没有解决,但我只想知道向量错误。 谢谢你。

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

直方图.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);
    }
}

您正在访问元素1....6时可能应该是0...5 在您所在的行中遇到同样的问题

largeBin = binHistogram.at(i);

这很可能是导致错误的行(上面的行不会很好地告诉您问题是什么,但只会使您的程序崩溃)。

你永远不会改变aHistogram类中向量的大小,这意味着它的大小将始终为零。 任何索引都会越界。

对于直方图之类的东西,我实际上建议您使用std::unorderd_map而不是std::vector ,“面”是键,计数是数据。 然后你可以做例如

binHistogramMap[face] += 1;

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

暂无
暂无

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM