【问题标题】:Changing Enumerations after declaring once c++声明一次 C++ 后更改枚举
【发布时间】:2016-05-22 16:16:07
【问题描述】:

我正在从“Jumping into c++”一书中学习 c++,在枚举和 switch-case 章节之后,它说我们应该编写一个 Tictactoe 游戏。
我通过声明包含三个状态的 tictactoe 枚举器来编写游戏,即空白、o 和 x,并将 9 个棋盘位置声明为空白。但在测试程序后,我发现我无法将板位置从空白更改为任何东西。这是我的错误还是枚举的工作方式?

例如:

    enum Tictactoe{
    TTT_O, 
    TTT_X,
    TTT_Blank};
    Tictactoe Board1 = TTT_Blank;
    //goes all the way until board position 9
    int input; 
    cout << "X, chose a pile which you want to mark << endl;
    cin>> input; 
    switch (input) {
    case 1: { Tictactoe Board1 = TTT_X; break; }
    //again, goes all the way until all the board positions are checked. 

在代码中,它确实每 2 步打印一次棋盘,但它总是将所有棋盘位置显示为空白。

【问题讨论】:

  • 请学习如何创建一个Minimal, Complete, and Verifiable Example,您可以向我们展示,它会更容易为您提供帮助。
  • 您能否至少展示一下您尝试过的操作,因为您不清楚枚举的哪一部分。
  • 不,这不是枚举的工作方式。是的,这是代码中的错误。

标签: c++ enums codeblocks enumeration


【解决方案1】:

你想要

case 1: { Board1 = TTT_X; break; }

这样你就改变了现有的变量。使用Tictactoe Board1 = TTT_X;,您正在声明一个新变量,然后立即销毁该变量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-20
    • 1970-01-01
    • 2015-04-26
    • 2020-07-19
    • 1970-01-01
    • 2014-08-22
    • 2010-09-09
    相关资源
    最近更新 更多