C++ 错误:“char”之前应为主表达式

C++ 错误:“char”之前应为主表达式,c++,char,boolean,C++,Char,Boolean,试图在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&

试图在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";

                    }
            }
        }
}

如何调用yes函数存在一些小的语义问题。试试这个:

bool yesResult = yes(c);

调用该方法时不包含char是正确的。当您将c传递给函数调用时,不再需要它-c本质上是一个字符。另一件需要注意的事情是,在第一次调用yes时,在调用之前指定了bool。只有当您想存储函数的返回值时,才需要这种语法,如我的示例所示。请注意,类型需要附带名称。你的其他调用,如果是语句条件,那么是正确的。< /p>阅读C++的任何教程,学习如何调用函数。提示:不像yeschar c。尝试的目的是什么?另外,在if语句中不需要==true在您的示例中,它没有任何作用。正如前面提到的,您的函数调用语法是错误的。if yeschar c==true应该是if yesc==true。最重要的是,比较布尔值是否等于false或true是不必要的。它已经是一个布尔值了;只要测试一下:如果yescAs c不断变化,那么将yesc的结果赋给常量变量就没有意义了。继续打电话给yesc,说得好。我没有读剩下的代码。const关键字已通过编辑删除。返回值不需要存储在变量中。在OP的代码中直接使用它是可以的,显然不需要这样做。然而,OP的第一次yes调用似乎有点混乱,因为它在调用之前不必要地指定了一个类型。我的愿望是在这样的语法有效时显示OP。在该点之后,每次调用yes确实不需要存储在变量中;不是调用-它是函数的原型声明,在该上下文中是冗余的。由于这种混乱,我建议OP将其删除。