0

Say you had two classes "TwoDice" and "Die". You want two Die objects to always be part of an instance of TwoDice, so you create two Die objects in the constructor.

TwoDice::TwoDice()
{
    Die die1;
    Die die2;
}

Then you call the TwoDice object's rollDice method which in turn calls on each individual Die's roll method.

bool TwoDice::rollDice()
{
    faceValue1 = die1.roll();
    faceValue2 = die2.roll();
}

Currently, the issue is that when I set it up this way, die1 and die2 are not defined which makes sense because those are just local variables within that constructor. However, when I made die1 and die2 specifically defined private variables for the TwoDice class, I received multiple compile errors. Is there a way I can make those two Die objects public so the other methods can access them?

Here's the TwoDice.cpp file:

// TwoDice.cpp: TwoDice class method definitions

#include "stdafx.h"
#include "time.h"
#include "TwoDice.h"
#include "Die.cpp"
#include "Die.h"
#include <cstdlib>
#include <iostream>

using namespace std;

TwoDice::TwoDice(void)
{
    Die die1;
    Die die2;
}

TwoDice::TwoDice(int d1, int d2)
{
    Die die1(d1);
    Die die2(d2);
}

void TwoDice::rollDice(void)
{
    die1.roll();
    die2.roll();
}

void TwoDice::getFaceValueDieOne(void)
{
    faceValueDie1 = die1.getFaceValue();
}

void TwoDice::getFaceValueDieTwo(void)
{
    faceValueDie2 = die2.getFaceValue();
}

bool TwoDice::isMatchingPair(void)
{
    if(faceValueDie1 == faceValueDie2)
    {
        return true;
    }
    else
    {
        return false;
    }
}

bool TwoDice::isSnakeEyes(void)
{
    if(faceValueDie1 == 1 && faceValueDie2 == 1)
    {
        return true;
    }
    else
    {
        return false;
    }
}

void TwoDice::display(void)
{
    cout << "Die 1 = " << faceValueDie1 << endl;
    cout << "Die 2 = " << faceValueDie2 << endl;
}

int TwoDice::getValueOfDice()
{
    return faceValueDie1 + faceValueDie2;
}

And here's the TwoDice.h file:

// TwoDice.h: class definition file

#pragma once
class TwoDice
{
    private:
        int faceValueDie1;
        int faceValueDie2;
    public:
        TwoDice();
        TwoDice(int, int);      
        void rollDice();
        void getFaceValueDieOne();
        void getFaceValueDieTwo();
        bool isMatchingPair();
        bool isSnakeEyes();
        void display();
        int getValueOfDice();
};

Here is Die.cpp:

// Die.cpp: Die class method definitions

#include "stdafx.h"
#include "time.h"
#include "Die.h"
#include <cstdlib>

using namespace std;

Die::Die(void)
{
    numSides = 6;
    faceValue = 0;
    srand((unsigned int)time(NULL));
}

Die::Die(int n)
{
    numSides = n;
    faceValue = 0;
    srand((unsigned int)time(NULL));
}

int Die::roll()
{
    faceValue = rand()%numSides + 1;

    return faceValue;
}

int Die::getFaceValue()
{
    return faceValue;
}

Here is Die.h:

// Die.h: class definition file

#pragma once
class Die
{
    private:
        int numSides;
        int faceValue;
    public:
        Die();
        Die(int n);
        int roll();
        int getFaceValue();
};
5
  • 1
    You should put the code with the member variables at least since this code is simply invalid in its design. Commented Aug 27, 2013 at 19:11
  • Why are the Die instances not member vars? Commented Aug 27, 2013 at 19:12
  • 2
    #include "Die.cpp" : why would you do that ? Commented Aug 27, 2013 at 19:13
  • As Adam Galarneau has already written, please show us the code for "However, when I made die1 and die2 specifically defined private variables for the TwoDice class, I received multiple compile errors.". And show us the compile errors. Commented Aug 27, 2013 at 19:19
  • Thank you for getting the singular and plural correct. Commented Aug 27, 2013 at 19:42

3 Answers 3

3

All you constructor code seems to declare stack variables. Change the class to have Die member variables.

class TwoDice
{
    private:
        int faceValueDie1;
        int faceValueDie2;
        Die die1;
        Die die2;
        // then as your code

Then change the constructors as follows

TwoDice::TwoDice()
{
}

TwoDice::TwoDice(int d1, int d2)
  : die1(d1),
    die2(d2)
{
}

The second constructor tells the Die constructor which int to use

Sign up to request clarification or add additional context in comments.

3 Comments

and OP must not forget to include Die.h before declaring the TwoDice class. I'm pretty sure the "multiple compile errors" come from having forgotten to move the #include declaration.
Let's call them "local variables". There's no reason that your member variables can't also be on the stack.
Fair point. "local" is a far more precise way of expressing this
1

Make them member variables.

class Die
{
public:
  // construct the die, given a face value
  Die(int value) : face_value_(value)
  {
  }

  int get_value() const
  {
     return face_value_;
  }

  // randomize the face value based on rolling
  void roll()
  {
     face_value_ = 1 + (rand() % number_of_faces_);
  }

private:
  int face_value_;
};

// encapsulates two dice    
class TwoDice
{
public:
   // construct each one (one_ and two_) with a specific starting value
   TwoDice(int fv_one, int fv2) : one_(fv_one), two_(fv_two)
   {
   }

   // roll (both)
   void roll()
   {
       one_.roll();
       two_.roll();
   }

   // These objects are member variables.  They are
   // owned by a specific instance of this class.
   Die one_;
   Die two_;
};

Comments

0

Those objects go out of scope and are destructed at the end of your constructor. To use them elsewhere in the class, declare them as member variables.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.