【问题标题】:Incremented variable "never used"?增量变量“从未使用”?
【发布时间】:2018-07-21 10:13:22
【问题描述】:

我对 C++ 有点缺乏经验,我正在将我用 C 编写的程序转换为 C++。我有一个 RollDice 函数,它接受我从文本文件中读取的数字并使用它们来生成数字。这是 C 语言中的函数:

void rollDice(Move *move, GameState *game_state) {
    int diceNum1 = 0;
    int diceNum2 = 0;
    int randomNumber1 = 0;
    int randomNumber2 = 0;
    randomNumber1 = game_state->randomNums[game_state->current_roll]; //gets the random number from the array randomNum (which holds the numbers from the text file), at index "current_roll"
    game_state->current_roll++; //increments so the next random number will be the next number in the array
    diceNum1 = 1 + (randomNumber1 % (1 + 6 - 1));
    randomNumber2 = game_state->randomNums[game_state->current_roll];
    game_state->current_roll++;
    diceNum2 = 1 + (randomNumber2 % (1 + 6 - 1));
    move->dice_sum = diceNum1 + diceNum2;
    printf("You rolled a %d!\n", move->dice_sum);
}

当我运行它时,这正是我想要的。现在,当我将我的程序转换为 C++ 时,我不得不改变一些事情。我的参数现在通过引用传递,我创建了一个向量来存储文本文件中的随机数列表:

void rollDice(Move& move, GameState& game_state) {
    std:: vector<int> randomNums = game_state.getRandomNums();
    int current_roll = game_state.getCurrentRoll();
    int diceNum1 = 0;
    int diceNum2 = 0;
    int randomNumber1 = 0;
    int randomNumber2 = 0;
    randomNumber1 = randomNums.at(current_roll);
    current_roll++;
    diceNum1 = 1 + (randomNumber1 % (1 + 6 - 1));
    randomNumber2 = randomNums.at(current_roll);
    current_roll++;   //this line is grayed out and says "this value is never used"
    diceNum2 = 1 + (randomNumber2 % (1 + 6 - 1));
    move.dice_sum = diceNum1 + diceNum2;
    std:: cout << "You rolled a " << move.dice_sum << "!\n";
}

我的代码告诉我第二次增加 current_roll 时它没有被使用。我的 C 代码没有发生这种情况,那么为什么会在这里发生,我该如何解决呢?我完全迷路了。

【问题讨论】:

  • 但是我需要那行代码让程序知道在下次掷骰子时增加数字。有没有办法以某种方式实现它?
  • 你真的不知道。一旦退出该方法,该值就会丢失。并且您不会在该行之后的方法中使用它。

标签: c++ c increment unused-variables


【解决方案1】:

它从未使用过,因为您写入变量,但从未读取它。拥有一个你从未读过的变量实际上是没有意义的。

大概你的game_state.getCurrentRoll 函数返回一个整数,当你存储它时,你存储 (而不是对值的引用),因此增加它不会增加当前滚动里面game_state,相反,您应该向您的 game_state 添加一个名为 makeRoll 的函数,例如增加 game_states 内部 current_roll 值。

这与您的 C 代码不同,后者使用 game_state-&gt;current_roll++直接递增 current_roll 值(或者,您可以将 game_state.current_roll 设为 public 并以与 C 代码中相同的方式递增它) .

根据您的评论,我假设您有一些课程:

class GameState {
private:
    int current_roll;
    ...
public:
    int getCurrentRoll() {
        return current_roll;
    }
    ...
}

你需要做的就是在你的类中添加另一个函数来增加current_roll

class GameState {
private:
    int current_roll;
    ...
public:
    int getCurrentRoll() {
        return current_roll;
    }
    void makeRoll() {
        current_roll++;
    }
    ...
}

那你就可以正常调用了。


关于您在 cmets 中关于错误的新问题:

参数类型不匹配:对“int”类型的有符号值使用“unsigned long”。

这是因为at 的签名是std::vector::at( size_type pos );,也就是说,它需要一个size_type 类型的值,它是一个无符号整数类型,而不是你使用的int,它是有符号的。 This 帖子可能会有所帮助。

【讨论】:

  • 谢谢。因此,假设我创建了一个函数,它像您建议的那样在内部增加 current_roll 值 - 类似于“int makeRoll(int current_roll)”。由于我将在我的 game_state 类中进行此操作,因此我是否必须使其类似于 getter/setter 函数,因为我的对象是私有的?
  • @JihaneEter 见编辑,我的 C++ 有点生锈,所以可能不会成功。添加的函数不需要返回任何内容或接受任何参数,因为current_roll 在实例化类中
  • 我认为应该这样做。谢谢!另一个不相关的问题,但在 RollDice 的同一个函数中,我一直在这一行得到一个注释:“randomNumber1 = randomNums.at(current_roll);”,说“参数类型不匹配:使用 'unsigned long' 的有符号值输入'int'。”它指的是current_roll。有任何想法吗?我的两个变量都是 int 类型的
  • @JihaneEter 添加了一些可能有帮助的信息,您可以尝试使用randomNums[current_roll],但我的 C++ 又生锈了
猜你喜欢
  • 2012-03-19
  • 2021-06-23
  • 2023-03-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-17
相关资源
最近更新 更多