简体   繁体   English

有符号和无符号整数表达式S之间的比较

[英]Comparison between signed and unsigned integer expressions S

I get this warning when compiling a source in c ++ with gcc in FreeBSD. 编译在C源时++在FreeBSD的GCC我得到这样的警告。

Could someone explain and help me solve the issue I'm having? 有人可以解释并帮助我解决我遇到的问题吗?

Following is a link to the entire source code, it was placed on pastebin because it contains 7000 lines of code. 以下是整个源代码的链接,由于将其包含7000行代码,因此将其放置在pastebin上。 Source char.cpp 来源char.cpp

Here is the warning message : 这是警告消息:

In member function 'void CHARACTER::PointChange(BYTE, int, bool, bool)':

Expanding on @KaliG s answer: 扩展@KaliG的答案:

On the line 3065 you are declaring: DWORD exp = GetExp(); 3065行上,您需要声明: DWORD exp = GetExp();

Now what is a DWORD ? 现在什么是DWORD Well, it stands for double word , and a "word" is 16 bits on this C++ implementation (Win32). 嗯,它代表double word ,在此C ++实现(Win32)上,“ word”为16位。 It is a typedef and is actually an unsigned integer . 它是一个typedef,实际上是一个无符号整数 http://en.wikipedia.org/wiki/Word_%28computer_architecture%29 http://en.wikipedia.org/wiki/Word_%28computer_architecture%29

The other variable, amount is an argument defined as the type (signed) int . 另一个变量amount是定义为int类型(有符号)的参数。

So you are comparing a signed and unsigned integer - which causes the warning. 因此,您正在比较有符号和无符号整数-这会引起警告。 You can solve this by simply casting amount to an unsigned int (or "DWORD") since you have verified already that it is in fact positive. 您可以通过简单地将数量转换为无符号的int(或“ DWORD”)来解决此问题,因为您已经验证了它实际上是正数。

So change the line to: 因此,将行更改为:

if (amount < 0 && exp < (DWORD) -amount)

This should work - but I have no idea how your method works other than that. 这应该可以工作-但除此之外,我不知道您的方法如何工作。

Sidenote: Hungarian notation is really ghastly stuff - so you should really dig into what the different type names they use actually are. 旁注: 匈牙利表示法确实是令人毛骨悚然的东西-因此,您应该真正研究一下它们实际使用的不同类型名称。 http://en.wikipedia.org/wiki/Hungarian_notation http://en.wikipedia.org/wiki/匈牙利语

Sidenote 2: Don't use ALLCAPS class names... developers are used to think that those identifiers are constants, so you confuse other people who might read your code. 旁注2:不要使用ALLCAPS类名...开发人员常常认为这些标识符是常量,因此您会使其他可能会读取代码的人感到困惑。

Sidenote 3: Read up on 2s complement to understand what the ALU ( http://en.wikipedia.org/wiki/Arithmetic_logic_unit ) inside the CPU is actually doing: http://en.wikipedia.org/wiki/Two%27s_complement 旁注3:以2s补码形式阅读,以了解CPU内部的ALU( http://en.wikipedia.org/wiki/Arithmetic_logic_unit )的实际作用: http : //en.wikipedia.org/wiki/Two%27s_complement

From the error thrown, I would say it is because 'exp' is either an unsigned or signed variable while 'amount' is the opposite, hence the reason you get the comparison error thrown. 从抛出的错误中,我会说这是因为'exp'是一个无符号或有符号的变量,而'amount'是相反的,因此是您抛出比较错误的原因。

Please post the lines of code where you declare these variables. 请在声明这些变量的地方张贴代码行。 :) :)

(Verify if you declared either of these 2 variables as a signed/unsigned by mistake.) (验证是否错误地将这两个变量中的一个声明为有符号/无符号。)

暂无
暂无

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

相关问题 警告 - 有符号和无符号整数表达式之间的比较 - A warning - comparison between signed and unsigned integer expressions 有符号和无符号整数表达式的比较 - Comparison between signed and unsigned integer expressions 查找文件中最长的行,有符号和无符号整数表达式之间的比较 - 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 警告:有符号和无符号整数表达式之间的比较 [-Wsign-compare] - warning: comparison between signed and unsigned integer expressions [-Wsign-compare] 如何忽略“有符号和无符号整数表达式之间的比较”? - How to ignore 'comparison between signed and unsigned integer expressions'? 在进行make install时,有符号和无符号整数表达式之间的比较警告? - comparison between signed and unsigned integer expressions warning while make install?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM