假设您有两个班级“TwoDice”和“Die”。您希望两个Die对象始终是TwoDice实例的一部分,因此您在构造函数中创建两个Die对象。
TwoDice::TwoDice()
{
Die die1;
Die die2;
}
然后调用TwoDice对象的rollDice方法,该方法依次调用每个Die的roll方法。
bool TwoDice::rollDice()
{
faceValue1 = die1.roll();
faceValue2 = die2.roll();
}
目前,问题是当我以这种方式设置时,die1和die2没有被定义,这是有意义的,因为那些只是该构造函数中的局部变量。但是,当我使die1和die2专门为TwoDice类定义私有变量时,我收到了多个编译错误。有没有办法让这两个Die对象公开,以便其他方法可以访问它们?
这是TwoDice.cpp文件:
// 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;
}
这是TwoDice.h文件:
// 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();
};
这是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;
}
这是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();
};
答案 0 :(得分:3)
所有构造函数代码似乎都声明了堆栈变量。将类更改为具有Die成员变量。
class TwoDice
{
private:
int faceValueDie1;
int faceValueDie2;
Die die1;
Die die2;
// then as your code
然后按如下所示更改构造函数
TwoDice::TwoDice()
{
}
TwoDice::TwoDice(int d1, int d2)
: die1(d1),
die2(d2)
{
}
第二个构造函数告诉Die
构造函数使用哪个int
答案 1 :(得分:1)
将它们作为成员变量。
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_;
};
答案 2 :(得分:0)
这些对象超出范围并在构造函数的末尾被破坏。要在类的其他地方使用它们,请将它们声明为成员变量。