【问题标题】:Cannot jump to label 'fin', error: from here, and crosses initialization无法跳转到标签“fin”,错误:从这里开始,并经过初始化
【发布时间】:2016-04-12 18:16:58
【问题描述】:

我最大的问题都在上面说了,无法跳转到标签fin(第27行错误),错误:从这里(第12和14行错误)和十字架初始化错误(第20行错误)请帮忙!

#include <iostream>
#include <string>

int main()
{
  std::string name;
  std::cout << "Please comply. y/n: ";
  std::string answer;
  std::cin >> answer;
  if (answer == "y"){std::cout << "You were spared." << std::endl; goto fin;}
  if (answer == "Miche"){std::cout << "The killers understood that you understood the prophecy, so they took you to their master" << std::endl; goto secret;}
  if (answer == "n"){std::cout << "You were brutally killed." << std::endl; goto fin;}
  else {std::cout << "You randomly babled " << answer << ", getting yourself killed."; goto fin;}
  secret:
  std::cout << "In order to fully find out if you are the legendary Miche, they took you to their leader."
  << " The master looked you over, and asked you one final question. The master asks you, fish?" << std::endl;

  std::string fish; fish = "none";
  std::cin >> fish;
  if (fish == "fish."){std::cout << "You were put in the throne of the king, where you ruled your near killers and their species for eternity."
  << std::endl; goto fin;}
  else {std::cout << "You failed and were immediately killed." << std::endl; goto fin;}
  goto fin;

  fin:

  return 0;
}

【问题讨论】:

  • 我们在您的帖子中看不到行号。请删除不相关的代码,并指向发生错误的地方。
  • Protip:不要使用goto;
  • 在某些情况下,人们想使用goto,但事实并非如此。您可以使用std::exit 或仅使用return 0;
  • @NathanOliver,我真的很想在 C++ 程序中看到 goto 的合理案例。如果您发布一个作为答案,我会亲自投票(当然,前提是它是合理的!)
  • @NathanOliver,是的,有点期待这个。但是适当的重构(例如将循环放入另一个函数并从中返回)通常会产生更好的结果。有些人主张throw让你摆脱循环,但我不赞成。

标签: c++


【解决方案1】:

问题本质上是这样的:

int main() {
    if (whatever)
        goto fin;
    std::string fish;
fin:
    return 0;
}

如果whatever 为真,goto 会跳过fish 的构造。这是不允许的,因为编译器无法生成合理的代码来销毁fish,具体取决于是否执行了 goto。

解决方案:不要使用 goto。

可能性:

int main() {
    if (whatever)
        goto fin;
    {
    std::string fish;
    }
fin:
    return 0;

这里,fish 在块的末尾被销毁,因此 goto 不会引起问题(除了其固有的非结构化性质)。

更好:

int main() {
    if (!whatever) {
        std::string fish;
    }
    return 0;
}

【讨论】:

    【解决方案2】:

    您可以使用更简单的函数复制问题。

    void foo()
    {
       goto fin;
       std::string fish = "none";
       std::cin >> fish;
    
       fin:
    }
    

    为什么会有这样的问题?当执行跳转到fin:时,初始化fish的代码没有被执行。它的析构函数将在函数返回时被调用。由于将在未初始化的对象上调用析构函数,因此程序将表现出未定义的行为。

    解决问题的最简单方法是将main 中的几乎所有内容放在另一个函数中,然后使用return 而不是goto

    void do_stuff()
    {
       std::string name;
       std::cout << "Please comply. y/n: ";
       std::string answer;
       std::cin >> answer;
    
       if (answer == "y")
       {
          std::cout << "You were spared." << std::endl;
          return;
       }
       if (answer == "n")
       {
          std::cout << "You were brutally killed." << std::endl;
          return;
       }
    
       if (answer == "Miche")
       {
          std::cout << "The killers understood that you understood the prophecy, so they took you to their master" << std::endl;
       }
       else
       {
          std::cout << "You randomly babled " << answer << ", getting yourself killed.";
          return;
       }
    
       std::cout << "In order to fully find out if you are the legendary Miche, they took you to their leader."
          << " The master looked you over, and asked you one final question. The master asks you, fish?" << std::endl;
    
       std::string fish = "none";
       std::cin >> fish;
       if (fish == "fish")
       {
          std::cout << "You were put in the throne of the king, where you ruled your near killers and their species for eternity." << std::endl;
       }
       else
       {
          std::cout << "You failed and were immediately killed." << std::endl;
       }
    }
    
    int main()
    {
       do_stuff();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-09
      • 2013-12-09
      • 2012-05-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-18
      • 1970-01-01
      相关资源
      最近更新 更多