I am not sure what to do for this error because I do not want to create an object that I initialize. I just want to use it to call the functions. I have included my header file and main() in case that I am doing something wrong.
It is complaining about the line that states: Dice roll; with the following error:
cpp(58) : error C2512: 'Dice' : no appropriate default constructor available
Header file:
class Dice
{
public:
// constant -- the indexes for the possible sums,
const static int possibleSums = 13;
Dice(const int []); // constructor to initialize the array
void rollDice();
void displayMessage();
static bool test();
private:
int results[possibleSums]; // declare the array, possible sums when we roll
};
Main() source file:
int main()
{
// array of sums initialized to counts of 0
int sumsArray [Dice::possibleSums] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
// create the object
Dice theDice(sumsArray);
// compute and display
//theDice.rollDice();
//theDice.displayMessage();
Dice::test();
// Don't close the screen until ENTER is pressed
//cout << endl << "Press ENTER to return..." << endl;
//cin.get();
return 0;
}
File with function definitions:
#include "Dice.h" // include definition of class Dice
//constructor to initialize results array
Dice::Dice(const int sumsArray[])
{
// initialize the elements of the array results to 0
for(int i = 0; i < possibleSums; i++)
results[i] = sumsArray[i]; // set the element at location / index i to 0
}
// function to roll the dice 36,000 times
void Dice::rollDice()
{
srand ( (unsigned)time ( 0 ) );
for( int i = 0; i < 36000; ++i ) // roll the 2 dice 36,000 times
{
int sum = 0;
sum += rand() % 6 + 1;
sum += rand() % 6 + 1;
++ results[sum];
}
//int results_7 = results[7];
//test(results_7);
}
// function to display the simulation outcome
void Dice::displayMessage()
{
// display a header, to keep the array information organized and informative
cout
<< "Sum Count" << endl
<< "-------------" << endl;
for( int i = 2; i <= 12; ++i )
{
// setw specifies the field width in which only the next value is to be output
cout << setw(3) << i << ": "<< setw(7) << results[i] << endl;
}
}
bool Dice::test()
{
Dice roll;
// compute and display
roll.rollDice();
int results_7 = roll.results[7];
if(results_7 >= 6,000)
{
cout << "Function passes.";
roll.displayMessage();
return true;
}
else if(results_7 < 6,000)
{
cout << "Function fails.";
return false;
}
}