【问题标题】:Calling bool function inside if parameters [closed]如果参数在内部调用布尔函数[关闭]
【发布时间】: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


【解决方案1】:

因为:EligibleForDiscount != EligibleForDiscount 加上一个额外的“i”,只是一个错字。

【讨论】:

  • 哦,哈哈,谢谢你指出这一点。
【解决方案2】:

可能是因为两个原因:

您在调用时拼错了函数名称:if (EligibleForDiscount(CompanyUsed, CompanySubscribed)) 应该像您的函数实现一样编写,即 EligibileForDiscount。

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

其中一个应该可以工作!

【讨论】:

  • 最好改函数名来匹配正确的拼写,而不是改函数调用来匹配拼写错误的名字。
  • 哦哈哈是的,这只是 Eligible 的拼写错误。谢谢
【解决方案3】:

附言你可以这样写EligibleForDiscount

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-21
    • 1970-01-01
    相关资源
    最近更新 更多