简体   繁体   English

C ++编译错误(有符号和无符号整数表达式之间的比较)

[英]C++ Compile Error (comparison between signed and unsigned integer expressions)

I need help regarding to this message: 我需要有关此消息的帮助:

char_cards.cpp: In member function 'void CHARACTER::Cards_pullout()': char_cards.cpp:88: warning: comparison between signed and unsigned integer expressions char_cards.cpp:在成员函数'void CHARACTER :: Cards_pullout()'中:char_cards.cpp:88:警告:有符号和无符号整数表达式之间的比较

Can somone explain what this error means? somone可以解释此错误的含义吗? I think the problem is with DWORD but i don't know what is wrong. 我认为问题出在DWORD,但我不知道出什么问题了。

This is the function: 这是功能:

DWORD CHARACTER::GetEmptySpaceInHand()
{
    for (int i=0; i<MAX_CARDS_IN_HAND; ++i)
    {
        if (character_cards.cards_in_hand[i].type == 0)
            return i;
    }
    return -1;
}
void CHARACTER::Cards_pullout()
{
    DWORD empty_space = GetEmptySpaceInHand(); 
    if (empty_space == -1) // Here is the error.
    {
        #ifdef __MULTI_LANGUAGE_SYSTEM__
        ChatPacket(CHAT_TYPE_INFO, LC_TEXT(GET_LANGUAGE(this), "You don't have space in hands."));
        #else
        ChatPacket(CHAT_TYPE_INFO, LC_TEXT("You don't have space in hands."));
        #endif
        return;
    }
    RandomizeCards();
    SendUpdatedInformations();
}

Initializing an unsigned integer to -1 is well defined and sets the unsigned integer to its maximal value. 将无符号整数初始化为-1的定义很明确,并将无符号整数设置为其最大值。 So using -1 to represent an error condition is OK. 因此,使用-1表示错误条件是可以的。 To get rid of the warning, you have few options: 要摆脱警告,您有几种选择:

1) Use a static_cast . 1)使用static_cast This indicates that you are aware of the conversion and it is intentional: 这表明您知道转换,这是有意的:

if empty_space == static_cast<DWORD>(-1)) { ...

2) Use std::numeric_limits<DWORD>::max() instead of -1 . 2)使用std::numeric_limits<DWORD>::max()代替-1 This will require including the limits header. 这将要求包括limits标头。

if (empty_space == std::numeric_limits<DWORD>::max()) { ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++有符号和无符号整数表达式之间的比较 - C++ Comparison between signed and unsigned integer expressions C++ 错误 - 警告:有符号和无符号整数表达式之间的比较 [-Wsign-compare] - C++ Error - warning: comparison between signed and unsigned integer expressions [-Wsign-compare] 警告 - 有符号和无符号整数表达式之间的比较 - A warning - comparison between signed and unsigned integer expressions 有符号和无符号整数表达式的比较 - Comparison between signed and unsigned integer expressions 有符号和无符号整数表达式S之间的比较 - Comparison between signed and unsigned integer expressions S 查找文件中最长的行,有符号和无符号整数表达式之间的比较 - Finding the longest line in a file, Comparison between signed and unsigned integer expressions 警告:有符号和无符号整数表达式之间的比较 [-Wsign-compare] - warning: comparison between signed and unsigned integer expressions [-Wsign-compare] 消除有符号和无符号整数表达式之间比较的绝佳方法 - Elegant way to get rid of comparison between signed and unsigned integer expressions 警告:有符号和无符号整数表达式之间的比较..如何解决? - warning: comparison between signed and unsigned integer expressions..how to solve it? 有符号和无符号整数表达式与0x80000000之间的比较 - comparison between signed and unsigned integer expressions and 0x80000000
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM