C++ 错误:“(”标记之前的预期标识符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30918875/
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
C++ error: expected identifier before "(" token
提问by Andrew
int nn1,nn2;
for (int i=1;i<=m;i++) if (A[i]>minim) && (A[i]<maxim) nn1++;
for (int j=1;j<=n;j++) if (B[j]>minim) && (B[j]<maxim) nn2++;
if (nn1>nn2) cout<<"1";
if (nn1<nn2) cout<<"2";
if (nn1=nn2) cout<<"0";
return 0;
}
can anybody give me a suggestion please why the compiler shows the error?
任何人都可以给我一个建议,为什么编译器会显示错误?
回答by Vlad from Moscow
In these if statements there are absent external parentheses
在这些 if 语句中没有外括号
for (int i=1;i<=m;i++) if (A[i]>minim) && (A[i]<maxim) nn1++;
for (int j=1;j<=n;j++) if (B[j]>minim) && (B[j]<maxim) nn2++;
I think that there should be
我认为应该有
for (int i=1;i<=m;i++) if ( (A[i]>minim) && (A[i]<maxim) ) nn1++;
for (int j=1;j<=n;j++) if ( (B[j]>minim) && (B[j]<maxim) ) nn2++;
And the loops look suspeciously. Take into account that array indices start from 0. So for example if you have an array of size N
then the valid range of indices is [0, N-1]
循环看起来很可疑。考虑到数组索引从 0 开始。例如,如果你有一个大小的数组,N
那么索引的有效范围是[0, N-1]
And you forgot to initialize nn1
and nn2
.
而你忘了初始化nn1
和nn2
。
It seems that you mean the following
看来你的意思是以下
int nn1 = 0, nn2 = 0;
for ( int i = 0; i < m; i++ )
{
if ( ( A[i] > minim ) && ( A[i] < maxim ) ) nn1++;
}
for ( int i = 0; i < n; i++ )
{
if ( ( B[i] > minim ) && ( B[i] < maxim ) ) nn2++;
}
if ( nn1 > nn2 ) cout << "1";
else if ( nn1 < nn2 ) cout << "2";
else cout << "0";
回答by animageofmine
Fix the brackets in if condition. Something like if (condition1 && condition2). I also suggest using curly brackets for every branch condition.
在 if 条件下固定括号。类似于 if (condition1 && condition2)。我还建议对每个分支条件使用大括号。
for (int i=1;i<=m;i++) {
if ((A[i]>minim) && (A[i]<maxim)) {
nn1++;
}
}
for (int j=1;j<=n;j++) {
if ((B[j]>minim) && (B[j]<maxim)) {
nn2++;
}
}
回答by Hashira
In C++,there should be a complete "()" after the "if" and what in "()" is the condition. So,the code you showed is if(A[i]>minim) && (A[i]minim) && (A[i]
在C++中,“if”后面应该有一个完整的“()”,“()”中的什么是条件。所以,你展示的代码是 if(A[i]>minim) && (A[i]minim) && (A[i]
回答by Ayushi Jha
Take care of paranthesis when dealing with ifs
:
处理时注意括号ifs
:
if ( (B[j]>minim) && (B[j]<maxim) )
| |
V V
Add these
So, your code should be:
所以,你的代码应该是:
int nn1,nn2;
for (int i=1;i<=m;i++) if ( (A[i]>minim) && (A[i]<maxim) ) nn1++;
for (int j=1;j<=n;j++) if ( (B[j]>minim) && (B[j]<maxim) ) nn2++;
if (nn1>nn2) cout<<"1";
if (nn1<nn2) cout<<"2";
if (nn1=nn2) cout<<"0";
return 0;
}
回答by Quentin
Missing parentheses around the whole condition :
整个条件周围缺少括号:
for (int i=1;i<=m;i++) if((A[i]>minim) && (A[i]<maxim)) nn1++;
// ^ ^
回答by Sourav Kanta
You forgot the ( ) in if statement.Try this :
你忘记了 if 语句中的 ()。试试这个:
int nn1,nn2;
for (int i=1;i<=m;i++) if ((A[i]>minim) && (A[i]<maxim)) nn1++; //note extra ()
for (int j=1;j<=n;j++) if ((B[j]>minim) && (B[j]<maxim)) nn2++; //note extra ()
if (nn1>nn2) cout<<"1";
if (nn1<nn2) cout<<"2";
if (nn1=nn2) cout<<"0";
return 0;
}