How do I set up my code to call a class object from another class?
I'm supposed to have it roll a die, get the face value, then use that portion to roll 3 dice and get the sum of those values.
My Code:
Die.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#ifndef DIE_H
#define DIE_H
class Die
{
public:
Die();
void Roll();
int GetFaces();
private:
int Face;
};
END OF CONDITIONAL BLOCK
#endif
Die.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include "Die.h"
#include <cstdlib>
Die::Die()
{
int Face = 0;
}
void Die::Roll()
{
Face = rand() % 6 + 1;
}
int Die::GetFaces()
{
return Face;
}
DiceRoll.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#ifndef DIEROLL_H
#define DIEROLL_H
class DiceRoll
{
public:
void DiceRoll();
int GetRollFaces();
private:
Die Die1;
Die Die2;
Die Die3;
};
#endif
And DiceRoll.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13
#include <iostream>
#include <cstdlib>
#include "DieRoll.h"
#include "Die.h"
void DiceRoll::DiceRoll()
{
this->Roll();
}
int DiceRoll::GetRollFaces()
{
// insert computation for 3 die
}
I see your logic, but changing the code as suggested gives me a plethora of errors. I'm going through them now to try to problem solve them one by one, but here they are:
dieroll.h(41): error C2380: type(s) preceding 'DiceRoll' (constructor with return type, or illegal redefinition of current class-name?)
dieroll.h(45): error C2146: syntax error : missing ';' before identifier 'Die1'
dieroll.h(45): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
dieroll.h(45): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
dieroll.h(46): error C2146: syntax error : missing ';' before identifier 'Die2'
dieroll.h(46): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
dieroll.h(46): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
dieroll.h(47): error C2146: syntax error : missing ';' before identifier 'Die3'
dieroll.h(47): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
dieroll.h(47): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
dieroll.cpp(55): error C2533: 'DiceRoll::{ctor}' : constructors not allowed a return type
dieroll.cpp(56): error C2065: 'Die1' : undeclared identifier
dieroll.cpp(56): error C2228: left of '.Roll' must have class/struct/union
1> type is ''unknown-type''
dieroll.cpp(57): error C2065: 'Die2' : undeclared identifier
dieroll.cpp(57): error C2228: left of '.Roll' must have class/struct/union
1> type is ''unknown-type''
dieroll.cpp(58): error C2065: 'Die3' : undeclared identifier
dieroll.cpp(58): error C2228: left of '.Roll' must have class/struct/union
1> type is ''unknown-type''
Die and DiceRoll are now both working to specifications, next I need to create an array to store the values and then for extra credit (as we haven't covered it yet) create a vertical histogram using '*' for the values. Any suggestions?
Well, I went ahead and kept working on my problems, and have most of them resolved. However, I have to take the data in the array and output it into a histogram (vertical and horizontal). Here's my code thus far. Could anyone assist me with the rest?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#ifndef DIE_H // allows for additional
#define DIE_H
class Die
{
public: // available outside of class
Die(); // SMF to set value
void Roll(); // member functions
int GetFaces();
private: // not available outside class
int Face;
};
#endif
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include "Die.h" // for processing die face
#include <cstdlib> // for the library rand() function
Die::Die() //Initializes Face data member
{
Face = 1;
}
void Die::Roll()
{
Face = rand() % 6 + 1;
}
int Die::GetFaces()
{
return Face;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#ifndef DIEROLL_H // allows for additional
#define DIEROLL_H
#include "Die.h"
class DiceRoll // classs that specifies a collection of 3 contained Die objects
{
public:
void RollDice(); // Calls Roll() function on each contained die
int GetRollFaces(); // Returns the sum of the current Face value of Die1, Die2, & Die3
private:
Die Die1; // The three die contained in this -i.e
Die Die2; // objects of this class automatically contain
Die Die3; // three dice
};
#endif
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream> // for cin,cout
#include <cstdlib> // for the library rand() function
#include "DieRoll.h" // for processing die roll
#include "Die.h"
void DiceRoll::RollDice()
{
Die1.Roll();
Die2.Roll();
Die3.Roll();
}
int DiceRoll::GetRollFaces()
{
int result;
result = Die1.GetFaces()+Die2.GetFaces()+Die3.GetFaces();
return result;
}
1 2 3 4 5 6 7 8
#ifndef SHELL_H // Avoid duplicate compilations
#define SHELL_H //
void GatherStats(int RollsArray[], int RollsArraySize, int ResultsArray[]); // 1st global function prototype
void DisplayResults(int ResultsArray[ ], int ResultsArraySize ); //2nd global function prototype
void createHistogram(int frequency[], int range); // function to display histogram
#endif
#include <iostream> // for cin,cout
#include <iomanip>
#include <cstdlib> // for the library rand() function
#include "FreeFunction.h" // for processing the roll
#include "DieRoll.h" // for processing die roll
usingnamespace std;
int main ()
{
constint NUM_RANGE = 15;
int frequency[NUM_RANGE+1];
int RollsArray[100];
DiceRoll TheIvories;
for (int i = 0; i < 100; i++)
{
TheIvories.RollDice(); // Roll the dice
RollsArray[i] = TheIvories.GetRollFaces(); // Record the result of the roll end loop
}
for (int j = 0; j< 100; j++)
{
cout << RollsArray[j]<< endl;
}
return 0;
}