DEV C++ 错误:'}' 标记前的预期声明

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5280939/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:48:40  来源:igfitidea点击:

DEV C ++ Error: expected declaration before '}' token

c++

提问by Francesca

What does this mean? My program goes like this: (NOTE: The line that has the error was the line coming before case 2.)

这是什么意思?我的程序是这样的:(注意:有错误的那一行是第 2 种情况之前的那一行。)

case 1:
{
cout<< "C * H * E * M * I * S * T * R * Y \n\n";
cout<< "1) What is the valence electron configuration of Selenium (Se)?\n\n";
cout<< "\na) 1s2 2s2 2p6 3s2\n\n";
cout<< "\nb) 1s2 2s2 2p2\n\n";
cout<< "\nc)4s2 4p4\n\n";
cout<< "\nd) 1s2 2s2 2p6 3s2 3p6 4s2 3d10 4p6 5s2 4d10 5p5\n\n";
cout<< "Enter your answer:\n";
cin>> answer;

if (answer == 'c')
{
    cout<<"Your answer is correct. Please press the enter key to proceed to the next question.\n\n";
}
else
    cout<< "The correct answer is C. Please press the enter key to proceed to the next question.\n\n";

getch ();
}

getch ();
cout<< "2) Which element yields the biggest atomic radius?\n\n";
cout<< "\na) Ca\n\n";
cout<< "\nb) Xe\n\n";
cout<< "\nc) B\n\n";
cout<< "\nd) Cs\n\n";
cout<< "Enter your answer:\n";
cin>> answer;
if (answer == 'd')
{
    cout<< "Your answer is correct. Please press the enter key to proceed to the next question.\n\n";
}
else
    cout<< "The correct answer is D. Please press the enter key to proceed to the next question.\n\n";

getch ();
}

cout<< "3) Name the ionic compound K2 Cr2 O7\n\n";
cout<< "\na) potassium chloride\n\n";
cout<< "\nb) potassium carbonate\n\n";
cout<< "\nc) potassium chromite\n\n";
cout<< "\nd) potassium chromate\n\n";
cout<< "Enter your answer:\n";
cin>> answer;
if (answer == 'd')
{
    cout<< "Your answer is correct. Please press the enter key to proceed to the next question.\n\n";
}
else
    cout<< "The correct answer is D. Please press the enter key to proceed to the next question.\n\n";


getch ();
}
}
case 2:
{
cout<< "G * E * O * M * E * T * R * Y \n\n";

The error, as noted in the title is expected declaration before '}' tokenat the line noted in my opening paragraph.

标题中指出的错误expected declaration before '}' token位于我开头段落中指出的行中。

回答by Tim Post

I think you want this:

我想你想要这个:

if (answer == 'd')
{
    cout<< "Your answer is correct. Please press the enter key to proceed to the next question.\n\n";
}
else
    cout<< "The correct answer is D. Please press the enter key to proceed to the next question.\n\n";


getch ();
}
}

To look like this:

看起来像这样:

if (answer == 'd')
{
    cout<< "Your answer is correct. Please press the enter key to proceed to the next question.\n\n";
} else {
    cout<< "The correct answer is D. Please press the enter key to proceed to the next question.\n\n";
    getch ();
}

It looks like you lost track of your scope immediately following the else.

看起来您在else.

回答by Bo Persson

There are two }'s right before the case. Where are the corresponding {'s? Especially the last elselooks a bit lonely.

之前有两个} case。对应的 { 在哪里?尤其是最后一张,else显得有些落寞。

回答by sarnold

After I reformatted your code so it would display tolerably on the website, the errors stand out pretty clearly.

在我重新格式化您的代码以便它可以在网站上正常显示后,错误非常明显。

I would recommend, when you use brackets around the ifclause, also use them around the elseclause:

我建议,当您在if子句周围使用括号时,也要在else子句周围使用它们:

 if (foo) {
     /* ... */
 } else {
     /* ... */
 }

Leaving off the brackets is quite fine if both cases are just one statement. And while there's no requirement about putting brackets around both clauses if only one needsthem, I have found plenty of bugs around ifstatements with brackets around only one path. It's surprising. :)

如果两种情况都只是一个陈述,则省略括号是很好的。虽然如果只有一个人需要它们,则没有要求在两个子句周围都放置括号,但我发现了很多if围绕仅在一条路径周围带有括号的语句的错误。令人惊讶。:)

On a further note, a text editor with bracket-matching capabilities would be a huge assistance. Many editors will highlight brackets that aren't properly nested, and some provide a key to jump between them quickly. (In viand clones, the %key will bounce between (), {}, []and <>pairs. Very handy.)

进一步说明,具有括号匹配功能的文本编辑器将是一个巨大的帮助。许多编辑器会突出显示未正确嵌套的括号,有些编辑器提供了在它们之间快速跳转的键。(在vi和克隆,该%键会之间反弹(){}[]<>对,非常方便。)

回答by Yury

Seems you have a superflous "}" after each getch:

每次获取后,您似乎都有一个多余的“}”:

 getch ();
 }
^^^