“else”之前的预计主表达式错误 [英] Expected primary-expression before "else" error
问题描述
我得到预期的主表达式之前的else
错误的第一个,我不明白为什么。我三检查所有的支撑。
I get the expected primary-expression before "else"
error for the first else and I do not understand why. I triple checked all the bracing.
if(Owin = win)
{
cout << "The computer has defeated you!\n\n";
cout << "Would you like to play again? (Y or N) : ";
cin >> restart;
if(restart == 'Y' || restart == 'y');
{
cout << "\n\n\n";
main() ;
}
else
{
cout << "Good Bye!\n\n";
system("pause");
return(0);
}
}
else
{
}
推荐答案
在您的代码中,
if(restart == 'Y' || restart == 'y');
尾部分号是空语句因此结束) if
块。在这一点上,你可能有一个 else
。
the trailing semicolon is an empty statement that forms the body of (and thus ends) the if
block. At this point you could have had an else
. But not after a subsequent statement that's not an if
.
更重要的是,更重要的是 >,在您的代码中,调用
Further, and more importantly, in your code, the call
main() ;
假设它调用全局中的 main
命名空间,无效。 main
(在全局命名空间中)是一个特殊函数。
assuming it calls the main
in the global namespace, is invalid. main
(in the global namespace) is a special function. Among its special properties is that it can't be called.
编译器可能允许它没有任何诊断,但正式你有未定义的行为
The compiler may allow it without any diagnostic, but formally you then have Undefined Behavior where anything can happen.
而是使用循环来实现重复。
Instead, use a loop to achieve repetition.
这篇关于“else”之前的预计主表达式错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!