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

Recommended Answers

All 4 Replies

In your headerfile put:

public:
    Dice(){};

You don't have a default constructor so that's why the compiler is complaining.

In your headerfile put:

public:
    Dice(){};

You don't have a default constructor so that's why the compiler is complaining.

Well would the following also be correct? I made some changes and by not using the constructor I was able to get it to work.

However, now I am wondering if I am testing the function for accuracy correctly? You would figure rolling a 7 would appear 6/36 times (so 6000 times out of the 36000) but this is not the case. I had to allot a 4% deviation in order to guarantee my function to pass and print the results / count for each sum.
I am not sure if I am doing this correctly and if I should be checking for something else?

Below is my code:

header file:

class Dice
{
public:
	const static int possibleSums = 13;

	//Dice(const int []);	// uncomment constructor 	

	void rollDice();			
	void displayMessage();		
	static bool test();		 // static member function to test

private:
	int results[possibleSums];	
};

main()

int main()
{
	Dice::test();
	
	cout << endl;
	return 0;
}

function definitions:

void Dice::rollDice() 
{
		for(int i = 0; i < possibleSums; i++)
		results[i] = 0;	// set the element at location / index i to 0

	srand ( (unsigned)time ( 0 ) );		
	for( int i = 0;  i < 36000;  ++i )	
	{
		int sum = 0;					
		sum += rand() % 6 + 1;			
		sum += rand() % 6 + 1;			
		++ results[sum];		
	}
}
// 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();

	//roll.displayMessage();

	int results_7 = roll.results[7];

	// originally i was testing for 6000, but to be sure, I allowed a deviation of 4%
	if(results_7 >= 5760)		
	{
		cout << "Function passes." << endl;
		cout<< endl;
		roll.displayMessage();
	}
	else if(results_7 < 5760)
	{
		cout << "Function fails."<< endl;
	}

	return true;
}

Well would the following also be correct? I made some changes and by not using the constructor I was able to get it to work.

However, now I am wondering if I am testing the function for accuracy correctly? You would figure rolling a 7 would appear 6/36 times (so 6000 times out of the 36000) but this is not the case. I had to allot a 4% deviation in order to guarantee my function to pass and print the results / count for each sum.
I am not sure if I am doing this correctly and if I should be checking for something else?

Well I got good results.

Sum     Count
-------------
  2:     971
  3:    1969
  4:    2971
  5:    4022
  6:    5097
  7:    5976
  8:    4967
  9:    4051
 10:    3040
 11:    1989
 12:     947
Function passes.

I didn't do any statistical tests with standard deviations and stuff, but these results look like they are in the ballpark. If you're looking to get exactly 6000 7's, the probability of that is very small. Your program seems to work. What made you decide on the 4% figure as opposed to 3% or 5% or something?

Well I got good results.

Sum     Count
-------------
  2:     971
  3:    1969
  4:    2971
  5:    4022
  6:    5097
  7:    5976
  8:    4967
  9:    4051
 10:    3040
 11:    1989
 12:     947
Function passes.

I didn't do any statistical tests with standard deviations and stuff, but these results look like they are in the ballpark. If you're looking to get exactly 6000 7's, the probability of that is very small. Your program seems to work. What made you decide on the 4% figure as opposed to 3% or 5% or something?

Basically I usually get about 5900 and greater for sum 7’s but there were a few occasions that I did get some in the 5700s and 5800s so I decided at most a 4% deviation because I had not seen anything lower after various runs.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.