【发布时间】:2014-04-24 13:12:45
【问题描述】:
所以我编写了一个 C++ bool 函数,如下所示:
bool EligibileForDiscount(const char CompanyUsed, const char CompanySubscribed)
{
bool eligible = false;
if (CompanyUsed==CompanySubscribed)
eligible = true;
return (eligible);
}
现在在我的 main() 中,这个函数被称为 if 语句的唯一参数:
if (EligibleForDiscount(CompanyUsed, CompanySubscribed))
{
ApplyDiscount(Cost, CompanySubscribed);
cout << "\nAfter your discount, your rental is: $"
<< fixed << showpoint << setprecision(2) << Cost << ".\n";
}
main函数是我老师写的,其他函数是我们写的,所以这个if语句不应该改。
所以我理解 if 语句试图完成什么,基本上是说“如果(真)这样做......”,因为 EligibleForDiscount 将返回一个布尔值。
但是,g++ 在 if 语句中给了我一个错误,告诉我 EligibleForDiscount 没有在这个范围内声明。
但我并不想将其用作值,而是用作对函数的调用。
【问题讨论】:
-
它是否在标题中声明而您忘记
#include它? -
我怀疑问题是错字。您最初将函数命名为
EligibileForDiscount(),与您所称的EligibleForDiscount()相差一个字母。 -
注意,函数中乱七八糟的代码可以简化为
return CompanyUsed==CompanySubscribed;
标签: c++ if-statement g++ boolean