我应该编写一个程序来模拟骰子(6 面)掷 6000 次并将结果存储在一个 vector 中。例如,如果掷骰子返回 1,我会做类似 frequency.at(0)++ 的事情。由于 vector 的大小将是固定的,而且我还需要能够自由访问每个元素,所以我想知道是否有使用默认构造函数或其他东西来声明 vector 的大小。这就是我目前拥有的,但我收到“函数调用中的参数太多”和“表达式必须具有类类型”的错误。也许我想做的事情是不可能的,我不知道,只是在寻求帮助。谢谢。
我的头文件:
#ifndef AHISTOGRAM_H
#define AHISTOGRAM_H
class aHistogram
{
public:
aHistogram();
~aHistogram();
void update(int face);
void display(int maxLengthOfLine);
void clear() const;
int count(int face);
private:
vector<int> numRolls();
int numx, m, j;
};
#endif
直方图.cpp:
#include <iostream>
#include <cstdlib>
#include <vector>
#include "aHistogram.h"
using namespace std;
aHistogram::aHistogram()
{
numRolls(6);
numx, m, j = 0;
}
aHistogram::~aHistogram()
{
}
void aHistogram::update(int face)
{
numRolls.at(face - 1)++;
return;
}
最佳答案
这是构造函数的初始化列表的用途:
aHistogram::aHistogram(): numRolls(6), numx(0), m(0), j(0) // constructor parameters here
{
// numRolls(6);
// numx m, j = 0;
}
而且你的 vector 声明在你的类定义中是错误的:
class aHistogram
{
public:
aHistogram();
~aHistogram();
void update(int face);
void display(int maxLengthOfLine);
void clear() const;
int count(int face);
private:
// vector<int> numRolls(); // this is declaring a function!
vector<int> numRolls; // USE THIS!!
int numx, m, j;
};
关于c++ - 在类中声明 vector 大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26959519/