【问题标题】:C++ Boolean function returning 56C++ 布尔函数返回 56
【发布时间】:2012-07-15 12:46:11
【问题描述】:

我有一个返回布尔值的函数:

bool restart()
{
    std::string answer;
    bool answered = false;
    while(answered == false)
    {
        cout << endl << endl << "Do you want to play again? y/n : ";
        cin >> answer;
        if(answer == "y" || answer == "Y" || answer == "1" || answer == "yes")
        {return true;}
        if(answer == "n" || answer == "N" || answer == "0" || answer == "no")
        {return false;}
    }
}

当我调用它时:

cout << restart();

我得到了输出:

Do you want to play again? y/n : y
56

谁能看到如何解决这个奇怪的问题? 提前致谢。

我现在的 WIP 代码:

#include <iostream>
#include <cstdlib>

using namespace std;

void drawScreen(int grid[3][3]);                               //
int movef(int grid[3][3], bool playersMove);
int updateGrid(int grid[3][3], int move, bool playersMove);
bool hasWon(int grid[3][3]);
bool swapMover(bool playersMove);                              //
bool restart();
void endGame();

int main()
{
    int grid[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
    bool appRunning = true, gameRunning = true, playersMove = true;
    int move;

    // tests //
    std::cout << restart();
    // tests //

    //while(appRunning == true)
    //{
    //    while(gameRunning == true)
    //    {
    //        drawScreen(grid);
    //        move = movef(grid, playersMove);
    //        grid[3][3] = updateGrid(grid, move, playersMove);
    //        drawScreen(grid);
    //        gameRunning = hasWon(grid);
    //        playersMove = swapMover(playersMove);
    //    }
    //    appRunning = restart();
    //}
    //endGame();
    }

    void drawScreen(int grid[3][3])
    {
        for(int i = 0; i < 3; i++)
        {
             for(int j = 0; j < 3; j++)
             {
             if(grid[i][j] == 10){cout << "X  ";if(j == 2){cout << endl << endl;}}
             if(grid[i][j] == 11){cout << "O  ";if(j == 2){cout << endl << endl;}}
             if(grid[i][j] != 10 && grid[i][j] != 11){cout << grid[i][j] << "  ";
             if(j == 2){cout << endl << endl;}}
             }
        }
   }

  int movef(int grid[3][3], bool playersMove)
  {
       return 0;
  }

  int updateGrid(int grid[3][3], int move, bool playersMove)
  {
       return 0;
  }

  bool hasWon(int grid[3][3])
  {
      return false;
  }

  bool swapMover(bool playersMove)
  {
       if(playersMove == true){return false;}
       if(playersMove == false){return true;}
  }

bool restart()
{
    std::string answer;
    bool answered = false;
    while(answered == false)
    {
       cout << endl << endl << "Do you want to play again? y/n : ";
       cin >> answer;
       if(answer == "y" || answer == "Y" || answer == "1" || answer == "yes")
       {return true;}
        if(answer == "n" || answer == "N" || answer == "0" || answer == "no")
       {return false;}
    }
}

void endGame()
{

}

【问题讨论】:

  • @user1487087:你能发布你的整个代码吗? (如果它足够小。)请包括包含的标头和其他预处理器指令。
  • @user1487087, 重现问题的最小完整样本是我们喜欢的。见sscce.org
  • @user1487087:发布的代码在 gcc4.6 中运行良好,所以问题一定出在其他地方。
  • @user1487087:你的完整代码可以工作perfectly fine on gcc4.3。你用的是什么编译器?
  • 我认为这不是问题所在,但是您没有在 while 循环之外定义返回值。如果以某种方式超出了它,则不会返回值(尽管我不知道在这种情况下会发生什么行为,或者即使在这种情况下会发生任何行为)

标签: c++


【解决方案1】:

我使用了 mingw32 编译器,在使用 g++ 4.8 时它工作正常。这似乎是一个编译器错误。 (感谢灰熊)

【讨论】:

    【解决方案2】:

    好吧,我猜。您的原始代码有该行

    grid[3][3] = updateGrid(grid, move, playersMove);
    

    你的网格定义是

    int grid[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
    

    这意味着您的写入超出了数组范围。这是未定义的行为。 请更正此问题并检查您的程序是否按预期工作。

    【讨论】:

      【解决方案3】:

      [这在某种程度上是对我的评论的重新发布,因为 OP 说它解决了他的问题]

      您没有在 while 循环之外定义返回值。如果您以某种方式超出它,则不会返回值(尽管我不知道预期的行为,或者即使在这种情况下预期有任何行为)

      为了让我的回答更彻底,我发现了这个: Why does flowing off the end of a non-void function without returning a value not produce a compiler error?

      【讨论】:

      • 确实 GCC 对此提出了警告。不过,我不知道它会如何传播出去。
      猜你喜欢
      • 2019-11-23
      • 1970-01-01
      • 2021-10-29
      • 2021-09-27
      • 2013-03-11
      • 1970-01-01
      • 2015-01-18
      • 2016-09-18
      • 2017-06-07
      相关资源
      最近更新 更多