[英]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.