错误:在 C++ 中返回之前预期的非限定 ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19190741/
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 : expected unqualified-id before return in c++
提问by user2774480
when I want to compile I get : Probléme : expected unqualified-id before "return" return 0; about last line : erreur:expexted declaration before { token
当我想编译时,我得到: 问题:在“返回”之前预期不合格的 id 返回 0;关于最后一行:错误:{令牌之前的expexted声明
I left the code unchanged just the middle part I changed... whats the problem??? here is my code:
我保留了代码不变,只是我更改了中间部分......有什么问题???这是我的代码:
#include <iostream>
using namespace std;
int main()
{
cout << "Pensez a un personnage : Mlle Rose, le Professeur Violet, "
<< "le Colonel Moutarde," << endl
<< "le Reverend Olive ou Mme Leblanc." << endl << endl;
cout << "Votre personnage a-t-il des moustaches (1 : oui, 0 : non) ? ";
bool moustaches;
cin >> moustaches;
cout << "Votre personnage porte-t-il des lunettes ? ";
bool lunettes;
cin >> lunettes;
cout << "Votre personnage porte-t-il un chapeau ? ";
bool chapeau;
cin >> chapeau;
cout << "Est-ce que votre personnage est un homme ? ";
bool homme;
cin >> homme;
cout << "==> Le personnage auquel vous pensez est ";
if (chapeau) {
/*******************************************
* Completez le programme a partir d'ici.
*******************************************/
cout << "le Professeur Violet";
else if (moustaches) {
cout << "le Colonel Moutarde";
}
else if (not lunettes) {
cout << "Mlle Rose";
}
else if (homme) {
cout <<"le Révérend Olive";
}
else {
cout <<"Mme Leblanc";
}
/*******************************************
* Ne rien modifier apres cette ligne.
*******************************************/
}
cout << endl;
return 0;
}
----------
回答by Niko
Just for the sake of people who landed here for the same reason I did:
只是为了那些和我一样的原因登陆这里的人:
Don't use reserved keywords
不要使用保留关键字
I named a function in my class definition delete(), which is a reserved keyword and should not be used as a function name. Renaming it to deletion() (which also made sense semantically in my case) resolved the issue.
我在我的类定义中命名了一个函数 delete(),它是一个保留关键字,不应用作函数名称。将其重命名为 delete() (在我的情况下在语义上也有意义)解决了这个问题。
For a list of reserved keywords: http://en.cppreference.com/w/cpp/keyword
有关保留关键字的列表:http: //en.cppreference.com/w/cpp/keyword
I quote: "Since they are used by the language, these keywords are not available for re-definition or overloading. "
我引用:“由于它们被语言使用,这些关键字不可用于重新定义或重载。”
回答by 0x499602D2
if (chapeau) {
You forgot the ending brace to this if
statement, so the subsequent else if
is considered a syntax error. You need to add the brace when the if
statement body is complete:
您忘记了此if
语句的结束大括号,因此后续语句else if
被视为语法错误。当if
语句体完成时,您需要添加大括号:
if (chapeau) {
cout << "le Professeur Violet";
}
else if (moustaches) {
cout << "le Colonel Moutarde";
}
// ...
回答by CS Pei
You need to move " }
" before the line of cout << endl;
to the line before the first else
.
您需要将 " }
" 之前的行移动cout << endl;
到第一个之前的行 else
。
回答by ChuckCottrill
Suggestions:
建议:
- use consistent 3-4 space indenting and you will find these problems much easier
- use a brace style that lines up {} vertically and you will see these problems quickly
- always indent control blocks another level
- use a syntax highlighting editor, it helps, you'll thank me later
- 使用一致的 3-4 个空格缩进,你会发现这些问题更容易
- 使用垂直排列 {} 的大括号样式,您会很快看到这些问题
- 总是缩进控制阻止另一个级别
- 使用语法高亮编辑器,它会有所帮助,稍后您会感谢我
for example,
例如,
type
functionname( arguments )
{
if (something)
{
do stuff
}
else
{
do other stuff
}
switch (value)
{
case 'a':
astuff
break;
case 'b':
bstuff
//fallthrough //always comment fallthrough as intentional
case 'c':
break;
default: //always consider default, and handle it explicitly
break;
}
while ( the lights are on )
{
if ( something happened )
{
run around in circles
if ( you are scared ) //yeah, much more than 3-4 levels of indent are too many!
{
scream and shout
}
}
}
return typevalue; //always return something, you'll thank me later
}