[英]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.