C++ 错误:标签“foo”已使用但未定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16852272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ERROR: Label "foo" used but not defined
提问by HurricaneFist
So I was fooling around with some C++ and got the previously stated error with some code that looked kind of like this:
所以我在玩一些 C++ 并使用一些看起来像这样的代码得到了先前陈述的错误:
#include <iostream>
using namespace std;
char foodstuffs;
void fruit()
{
cin>>foodstuffs;
switch(foodstuffs)
{
case 'a': goto foo; break;
case 'b': goto fooo; break;
}
}
int main()
{
cout<<"What do you want to eat? (a/b)";
fruit();
foo: cout<<"You eat an apple.";
fooo: cout<<"You eat a banana.";
}
The exact code was much more complex, but this is just to show you the error I got.
确切的代码要复杂得多,但这只是为了向您展示我得到的错误。
Now I realize that everyone despises the "goto" statement for some reason, but my actual code is full of so many gotos that I don't really have the time/patience to go back and change them all. Also, I'm kind of a novice programmer and I find gotos and labels to be very easy to use.
现在我意识到出于某种原因,每个人都鄙视“goto”语句,但我的实际代码充满了太多的 goto,我真的没有时间/耐心回去更改它们。此外,我是一个新手程序员,我发现 goto 和标签非常易于使用。
My question is how can I predefine these labels so that the function fruit() knows what they are? Also, I need to do this without moving the labels out of the main function.
我的问题是如何预定义这些标签,以便函数 Fruit() 知道它们是什么?此外,我需要在不将标签移出主函数的情况下执行此操作。
回答by Some programmer dude
The goto
statement can only go to locally defined labels, it can't jump to other functions.
该goto
语句只能去本地定义的标签,它不能跳转到其他功能。
So the labels in main
will not be referenced, and the goto
statements in fruit
will not find the labels.
因此main
不会引用in 中的标签,并且goto
in 中的语句fruit
将找不到标签。
回答by kfsone
What you are tryingto do - jump between functions - is not valid for a whole slew of reasons, not least being object scope and life time, consider:
您正在尝试做的 - 在函数之间跳转 - 由于多种原因无效,尤其是对象范围和生命周期,请考虑:
void foo()
{
if(feelLikeIt)
goto foobar;
}
void bar()
{
std::string message = "Hello";
foobar:
cout << message << endl;
}
Jumping to foobar from foo is illegal because "message" won't exist.
从 foo 跳转到 foobar 是非法的,因为“消息”不存在。
So the language just plain doesn't let you do this.
所以简单的语言不允许你这样做。
Further, the way you are trying to use "goto" would prevent you from re-using the "fruit()" function because it always makes a decision about what to do with the selection rather than the function calling it. What if you wanted to do this:
此外,您尝试使用“goto”的方式会阻止您重新使用“fruit()”函数,因为它总是决定如何处理选择而不是调用它的函数。如果你想这样做怎么办:
cout<<"What do you want to eat? (a/b)";
fruit();
foo: cout<<"You eat an apple.";
fooo: cout<<"You eat a banana.";
cout<<"What does your friend want to eat? (a/b)";
fruit();
// oops, you just created a loop because fruit made the decision on what to do next.
What you ACTUALLY want to do is use "fruit()" as a function that returns a value.
您实际上想要做的是使用“fruit()”作为返回值的函数。
enum Fruit { NoSuchFruit, Apple, Banana };
Fruit fruit(const char* request)
{
char foodstuffs;
cout << request << " (a/b)";
cin >> foodstuffs;
switch (foodstuffs)
{
case 'a': return Apple;
case 'b': return Banana;
default:
cout << "Don't recognize that fruit (" << foodstuffs << ")." << endl;
return NoSuchFruit;
}
}
const char* fruitNames[] = { "nothing", "an apple" ,"a banana" };
int main()
{
Fruit eaten = fruit("What do you want to eat?");
cout << "You ate " << fruitNames[eaten] << "." << endl;
eaten = fruit("What does your friend want to eat?");
cout << "Your friend ate " << fruitNames[eaten] << "." << endl;
}
回答by Mark Garcia
Sorry. You cannot goto
to a label that is outside of the current executing function. Also, there are other limits on the uses of goto
. For example, you cannot skip a variable definition using goto
. And there are others that I am not fully aware of.
对不起。您不能goto
使用当前正在执行的函数之外的标签。此外,对 的使用还有其他限制goto
。例如,您不能使用 跳过变量定义goto
。还有其他一些我并不完全了解。
The bottom line?
底线?
Don't use goto
.
不要使用goto
.
回答by Navneet
goto
cannot be used to navigate outside the current function. Try returning something from the function and use that in if else condition.
goto
不能用于在当前函数之外导航。尝试从函数返回一些东西并在 if else 条件中使用它。