我有一个类的函数成员,该成员应在执行时增加成员变量,但问题是它不能正常工作。因为输入值是随机创建的,所以我无法确切解释得到的值是什么
class Dice
{
public:
virtual void rollDice() = 0;
};
class Player : public Dice
{
public:
Player(){ };
Player(int id, string name ,int score = 0) : pId(id), pName(name) , pScore(score) { };
void rollDice();
int desplayScore() { return pScore;}
private:
int pId;
std::string pName;
int pScore;
unsigned char playerDiceRolled;
};
void Player::rollDice()
{
std::random_device rd;
std::mt19937 eng(rd());
std::uniform_int_distribution<> distr(1, 6);
this->playerDiceRolled = distr(eng);
std::cout << "player rolled:" << distr(eng) << "\n";
this->pScore =+ distr(eng);
}
int main()
{
Player A(1, "Player");
A.rollDice();
A.rollDice();
std::cout << A.desplayScore();
return 0;
}
根据随机值,结果会有所不同,但不是正确的结果。
答案 0 :(得分:3)
this->playerDiceRolled = distr(eng);
std::cout << "player rolled:" << distr(eng) << "\n";
this->pScore =+ distr(eng);
每次您调用发行版时,都会生成一个新的不同的伪随机数。您调用了三次,所以得到了三个不同的值。
如果您只是复制cout
行-或在调试器中单步执行程序-您会看到它的作用。
尝试这样的事情
this->playerDiceRolled = distr(eng);
std::cout << "player rolled:" << playerDiceRolled << "\n";
this->pScore += playerDiceRolled;
(包括Stephan注意到的+ =修复程序,我完全错过了)。
答案 1 :(得分:2)
您有错字
this->pScore =+ distr(eng);
不增加pScore
而是将其设置为该值。
您可能是想
this->pScore += distr(eng);
您应该重用随机生成的数字,而不要在rollDice
的过程中生成不同的数字:
this->playerDiceRolled = distr(eng);
std::cout << "player rolled:" << this->playerDiceRolled << "\n";
this->pScore += this->playerDiceRolled;
答案 2 :(得分:1)
您使用distr(eng)
三次,将根据this产生三种不同的输出。
您的逻辑在这里也是错误的。 Player
类不应是Dice
所固有的,而应具有一个对象。我将代码更改为:(注意移动 roll
Dice类的功能,并从 Player::rollDice
调用它)
class Dice
{
public:
unsigned char roll() {
std::random_device rd;
std::mt19937 eng(rd());
std::uniform_int_distribution<> distr(1, 6);
return distr(eng);
}
};
class Player
{
public:
Player(){ };
Player(int id, string name ,int score = 0) : pId(id), pName(name) , pScore(score) {
};
void rollDice();
int desplayScore() { return pScore;}
private:
int pId;
std::string pName;
int pScore;
unsigned char playerDiceRolled;
Dice mDice;
};
void Player::rollDice()
{
this->playerDiceRolled = mDice.roll();
std::cout << "player rolled:" << playerDiceRolled << "\n";
this->pScore += playerDiceRolled;
}
int main()
{
Player A(1, "Player");
A.rollDice();
A.rollDice();
std::cout << A.desplayScore();
return 0;
}
答案 3 :(得分:-1)
谢谢大家我修复了它
#include <iostream>
#include <memory>
#include <vector>
#include <string>
#include <random>
using namespace std;
class Dice
{
public:
virtual void rollDice() = 0;
};
class Player : public Dice
{
public:
Player(){ };
Player(int id, string name ,int score = 0) : pId(id), pName(name) , pScore(score) { };
void rollDice();
int desplayScore() { return pScore;}
private:
int pId;
std::string pName;
int pScore;
unsigned char playerDiceRolled;
};
void Player::rollDice()
{
std::random_device rd;
std::mt19937 eng(rd());
std::uniform_int_distribution<> distr(1, 6);
this->playerDiceRolled = distr(eng);
std::cout << "player rolled:" << to_string(playerDiceRolled) << "\n";
this->pScore += playerDiceRolled;
}
int main()
{
Player A(1, "Player");
A.rollDice();
A.rollDice();
std::cout << A.desplayScore();
return 0;
}