预期的初级表达之前' bool'

时间:2014-12-14 04:19:24

标签: c++

我的程序说"错误:在' bool'"之前预期的primary-expression;在主函数下的函数调用repeatOrNot (bool);上。这是为什么?

bool fiveOrNot(); 
void repeatOrNot (bool);

int main()
{
    fiveOrNot();
    repeatOrNot (bool);

    return 0;
}

bool fiveorNot()
{
    int number;
    cout << "Enter any number except for 5.\n";
    cin >> number;

    if(number == 5)
        return true;
    else
        return false;
}

void repeatOrNot(bool repeat)
{
    if(repeat == true)
        fiveorNot();
    else
        cout << ":(\n";
}

1 个答案:

答案 0 :(得分:1)

在C ++中。有所谓的形式参数实际参数。在定义函数时,您正在处理形式参数,这意味着您应该只指定参数的类型,并且(如果您愿意)给它一些有意义的名称

void repeatOrNot(bool repeat)//<----"repeat" is a formal parameter. 
//You are not passing anything to the function, 
//you are just telling the compiler that this function accepts one argument 
//of a bool type.
{
    if(repeat == true)
        fiveorNot();
    else
        cout << ":(\n";
}

但是,当你调用一个函数时,你需要传递一个实际参数,即一个正确类型的变量。在您的情况下,您可能想要将调用的结果传递给fiveOrNot函数。你可以这样做:

bool isFive = fiveOrNot();
repeatOrNot (isFive);//<---This is an actual parameter.
//here you are really passing a variable to your function.

或者像这样:

repeatOrNot(fiveOrNot());//<--the result of the execution of "fiveOrNot" will be passed into "repeatOrNot"