在if参数中调用bool函数



所以我写了一个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";
       }

主函数是我的老师写的,其他函数是我们写的,所以这个if语句不应该被修改。

所以我理解if语句试图完成什么,基本上说"if (true) do this…",因为EligibleForDiscount将返回一个布尔值。

然而,g++在if语句中给了我一个错误,告诉我在这个作用域中没有声明EligibleForDiscount。

但是我不是想把它作为一个值,而是作为一个函数的调用

这可能有两个原因:

你在调用函数时拼错了函数名:if (EligibleForDiscount(CompanyUsed, CompanySubscribed))应该像你的函数实现那样写,也就是EligibileForDiscount。

如果您忘记声明函数的原型,就会发生这种情况,这是程序将要使用该函数的指示符。在使用函数bool EligibileForDiscount(const char, const char)

之前,您只需要在某个地方写一下

其中一个应该可以!

Because: EligibileForDiscount != EligibleForDiscount加一个"i",只是个打字错误。

你可以这样写EligibleForDiscount:

bool EligibleForDiscount(const char CompanyUsed, const char CompanySubscribed)
{
 return CompanyUsed==CompanySubscribed;
}

最新更新