【问题标题】:'TRUE' was not declared in this scope C++'TRUE' 未在此范围 C++ 中声明
【发布时间】:2015-09-29 03:06:20
【问题描述】:

所以我有这个简单的函数,它应该返回真或假。只有 2% 的时间返回 True,每隔一次返回 false。但是,每当我尝试编译完整代码时,都会收到错误消息;

'TRUE' 未在此 C++ 范围内声明

我知道我可以使用int 而不是bool,并且只使用1 表示true,使用0 表示false,但我想弄清楚为什么这不起作用。有什么帮助吗?

这是我的功能:

bool Bunny::determineMutant()
{
    int rand_num = rand() % 100 + 1; //random num 1-100
    if(rand_num == 1 || rand_num == 2) return TRUE;
    else return FALSE;
}

【问题讨论】:

  • 尽量不要使用大写

标签: c++


【解决方案1】:

TRUEFALSE 通常是 associated with the Win32 type BOOL,或者是整数 10 的宏。由于TRUE 的定义不可用,因此存在实际错误。

对于built in C++ type bool 使用truefalse。我认为这应该是这里的首选解决方案。

附带说明,代码可以简化为根据 if() 中的条件返回,该条件已经是 bool

return (rand_num == 1 || rand_num == 2);

【讨论】:

    【解决方案2】:

    C++ 中的关键字是小写的truefalse

    全大写的TRUEFALSE一般是VC++中10的预处理宏,用于返回type BOOL

    【讨论】:

    • 我相信 Cocoa 也定义了它们。
    猜你喜欢
    • 2012-02-12
    • 2010-12-17
    • 1970-01-01
    • 2021-07-17
    • 2015-08-21
    • 2015-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多