C++ 如何解决此错误:跳转到案例标签交叉初始化
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23599708/
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
How do I resolve this error: jump to case label crosses initialization
提问by user3555274
I have the following error in my Calculator code and do not understand how to correct it. Please any advice would be helpful.
我的计算器代码中有以下错误,不知道如何更正。请任何建议都会有所帮助。
ERROR: error: jump to case label [-fpermissive]| error:crosses initialization of 'int sum'| error: 'exit' was not declared in this scope|
错误:错误:跳转到案例标签 [-fpermissive]| 错误:'int sum' 的交叉初始化| 错误:“退出”未在此范围内声明|
CODE:
代码:
#include <iostream>
#include <cmath>
using namespace std;
void display_menu();
int get_menu_choice();
void get_two_numbers(int &a, int &b);
int add(int a, int b);
int subtract(int a, int b);
int main()
{
int choice;
do
{
display_menu();
choice = get_menu_choice();
int x, y;
switch (choice)
{
case 1: get_two_numbers(x, y);
int sum = add(x, y);
cout << x << " + " << y << " = " << sum << endl;
break;
case 2: get_two_numbers(x, y);
int diff = subtract(x, y);
cout << x << " - " << y << " = " << diff << endl;
break;
default:;
}
} while (choice != 3);
cout << "Good bye...now." << endl;
return 0;
}
void display_menu()
{
cout << endl;
cout << "Simple Calculator Menu" << endl;
cout << "----------------------" << endl;
cout << " 1. Addition (+) " << endl;
cout << " 2. Subtraction (-) " << endl;
cout << " 3. Quit to exit the program" << endl;
cout << endl;
}
int get_menu_choice()
{
int choice;
cout << "Enter your selection (1, 2, or 3): ";
cin >> choice;
while(((choice < 1) || (choice > 3)) && (!cin.fail()))
{
cout << "Try again (1, 2, or 3): ";
cin >> choice;
}
if (cin.fail())
{
cout << "Error: exiting now ... " << endl;
exit(1);
}
return choice;
}
void get_two_numbers(int &a, int &b)
{
cout << "Enter two integer numbers: ";
cin >> a >> b;
}
int add(int a, int b)
{
return (a + b);
}
int subtract(int a, int b)
{
return (a - b);
}
回答by kfsone
You are declaring new variables inside a case statement without creating an enclosing scope:
您在 case 语句中声明新变量而不创建封闭范围:
switch (choice)
{
case 1: get_two_numbers(x, y);
//* vv here vv *
int sum = add(x, y);
//* ^^ here ^^ */
cout << x << " + " << y << " = " << sum << endl;
break;
case 2: get_two_numbers(x, y);
//* vv here vv */
int diff = subtract(x, y);
//* ^^ here ^^ */
cout << x << " - " << y << " = " << diff << endl;
break;
default:;
}
Here's how to fix it:
以下是修复方法:
switch (choice)
{
case 1:
{
get_two_numbers(x, y);
int sum = add(x, y);
cout << x << " + " << y << " = " << sum << endl;
}
break;
case 2:
{
get_two_numbers(x, y);
int diff = subtract(x, y);
cout << x << " - " << y << " = " << diff << endl;
}
break;
default:
break;
}
Of course, the exact formatting of brackets and indentation is up to you.
当然,括号和缩进的确切格式取决于您。
回答by Andrew McGuinness
A "case" of a switch doesn't create a scope, so, as the error says, you're jumping over the initialization of "sum" if the choice isn't 1.
开关的“case”不会创建作用域,因此,正如错误所说,如果选项不是 1,您将跳过“sum”的初始化。
You either need to declare sum and diff outside the switch, or create blocks with { } for each of the cases.
您需要在 switch 之外声明 sum 和 diff,或者为每种情况创建带有 { } 的块。
回答by Nassim
You should be declaring variables outside the switch statement and not inside a case. In particular sum
and diff
are problematic in your code. Once these variables are initialized outside the switch statement you can then set their values.
您应该在 switch 语句之外而不是在 case 内声明变量。特别是sum
并且diff
在您的代码中存在问题。一旦这些变量在 switch 语句之外初始化,您就可以设置它们的值。