"bool"之前的预期主要表达式
expected primary expression before 'bool'
我的程序在主函数下的函数调用repeatOrNot (bool);
上显示"错误:'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";
}
在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"
相关文章:
- 写入向量<向量<bool>>
- (C++)分析树以计算返回错误值的简单算术表达式
- 在VS2010-VS2015下编译时,如何使用decltype作为较大类型表达式的LHS
- 提升精神:解析布尔表达式并简化为规范范式
- 不能在初始值设定项列表中将非常量表达式从类型 'int' 缩小到'unsigned long long'
- 使用正则表达式regex_search在字符串中查找字符串
- 如何确认我的constexpr表达式实际上已经在编译时执行
- 概念中的cv限定符需要表达式参数列表
- 为什么constexpr的性能比正常表达式差
- 对于结构,表达式必须是可修改的ivalue
- 当一个值是非常量但用常量表达式初始化时使用constexpr
- 将fold表达式与std::一起用于两个元组
- C bool表达式作为函数参数调用错误的超载
- 在 lambda 表达式中自动推导出的类型是什么,用于修改 bool 类型的向量(特殊容器)
- When是bool a|=mayRun()的右手表达式;正在执行
- "bool"之前的预期主要表达式
- 在类方法上使用指针:表达式的类型必须为bool错误
- 在表达式 bool <<整数中,bool 是提升为 int,还是提升为与整数相同的类型?
- 为什么istream对象可以用作bool表达式
- 结构错误表达式必须为bool类型