KoolaidLips
KoolaidLips

Reputation: 267

Declaring vector size in a class

I am supposed to write a program that simulates a dice(6 faced) roll 6000 times and stores the results in a vector. For example if the dice roll returns 1, I would do something like frequency.at(0)++. Since the size of the vector is gonna be fixed, and I would also need to able to access each element freely, I was wondering if there was anyway to declare the size of the vector using a default constructor or something. This is what I currently have but I get a "too many arguments in function call" and "expression must have class type" error. Maybe what I'm trying to do is not possible, I don't know, but just looking for some help. Thanks.

My header file:

#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

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

Upvotes: 1

Views: 6121

Answers (2)

Masked Man
Masked Man

Reputation: 11045

IF the size of the "vector" is fixed, then using std::array is most certainly a better option, unless of course, you are using an ancient compiler which doesn't support C++11.

Go through the above link on cppreference.com. For most part, it is just like an "old fashioned" array, but it also comes with benefits such as bounds checking (if you use at() instead of operator[]), ability to iterate through elements (begin(), end(), etc.), and many other "benefits" of std::vector.

Have a look at this:

#include <iostream>
#include <array>
using namespace std;

class aHistogram {
public:
    aHistogram() : numRolls{0, 0, 0, 0, 0, 0} {}

    int count(int face) {
        return numRolls.at(face - 1);
    }

    void update(int face) {
        numRolls.at(face - 1)++;
    }

private:
    array<int, 7> numRolls;
};

int main() {
    aHistogram ah;
    ah.update(1);
    ah.update(2);
    ah.update(1);

    cout << "Count[1]: " << ah.count(1) << " Count[2]: " << ah.count(2) << endl;
}

Upvotes: 0

Galik
Galik

Reputation: 48625

This is what the constructor's initializer list is for:

aHistogram::aHistogram(): numRolls(6), numx(0), m(0), j(0) // constructor parameters here
{
    // numRolls(6);
    // numx m, j = 0;
}

Also the declaration of your vector is wrong in your class definition:

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;
};

Upvotes: 2

Related Questions