【发布时间】:2014-11-30 06:54:00
【问题描述】:
试图在int main 中调用函数bool yes()。不断收到上面显示的错误。我应该在函数调用中包含char c 吗?
bool yes(char c){
if(c == 'y' || c == 'Y'){
return true;
}
else
return false;
}
int main(){
try{
char c;
cin>>c;
bool yes(char c); //not sure if char c should be here
cout<<"Think of one of these 8 things: ..... Press '|' when you are ready\n";
if(c == '|'){
cout<<"Are you thinking of something big?\n";
cin>>c;
if(yes(char c) == true){ //error here in yes(), trying to call function
cout<<"Are you thinking of something that is alive?\n";
cin>>c;
if(yes(char c) == true){ //error here in yes(), trying to call function
cout<<"Are you thinking of an animal?\n";
cin>>c;
if(yes(char c) == true){ //error here in yes(), trying to call function
cout<<"You are thinking of an elephant.\n";
}
}
}
}
【问题讨论】:
-
阅读任何 C++ 教程并学习如何调用函数。提示:不像
yes(char c)。 -
try的目的是什么?另外,if 语句中不需要== true。 -
为了简化,删除这一行:
bool yes(char c);-- 在你的例子中没有任何作用。 -
如前所述,您的函数调用语法是错误的。
if (yes(char c) == true)应该是if (yes(c) == true)。最重要的是,没有必要将bool的相等值与false或true进行比较。它已经是一个布尔值;测试一下:if (yes(c))