错误:在' char'之前预期的primary-expression;

时间:2014-10-06 01:40:01

标签: c++ char boolean

尝试在bool yes()中调用函数int main。继续得到上面显示的错误。我应该在函数调用中包含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";

                    }
            }
        }
}

1 个答案:

答案 0 :(得分:0)

你如何调用你的&#34; yes&#34;会有一些轻微的语义问题。功能。试试这个:

bool yesResult = yes(c);

你没有包括&#34; char&#34;当您调用该方法时。将c传递给函数调用时不再需要它 - c本身就是一个char。另外需要注意的是,在你第一次调用yes()时,你有&#34; bool&#34;在通话前指定。只有在您想要存储函数的返回值时才需要这样的语法,如我的示例所示。请注意,类型需要附带名称。您在if语句条件中的其他调用yes()完全没问题。