简体   繁体   English

警告:逗号右手操作数无效gcc 4.4.7

[英]warning: right-hand operand of comma has no effect gcc 4.4.7

I need your help for the following warning in Linux gcc 4.4.7 对于Linux gcc 4.4.7中的以下警告,我需要您的帮助

for (int iLoop1= 0; iLoop1< iLoopN; ++iLoop1, ++iLoop2, !iIsOk)

I have the following warnings coming from the last argument after the last comma 我有以下警告来自最后一个逗号后的最后一个论点

error: right-hand operand of comma has no effect

I have read the wiki page on comma operator but I do not understand the problem https://en.wikipedia.org/wiki/Comma_operator 我已经阅读了有关逗号运算符的Wiki页面,但是我不理解该问题https://en.wikipedia.org/wiki/Comma_o​​perator

EDIT : effectively !iIsOk does nothing I have tested the following code 编辑:有效!iIsOk什么也不做,我已经测试了以下代码

// Example program
#include <iostream>
#include <string>
#include <iostream>

int main()
{

    int iIsOk = 0;

    for (int iLoop1 = 0; iLoop1 < 2; iLoop1++, !iIsOk)
    {
       std::cout << "IsOk=" << iIsOk << std::endl;
    }

      for (int iLoop1 = 0; iLoop1 < 2; iLoop1++, iIsOk = !iIsOk)
    {
       std::cout << "IsOk2=" << iIsOk << std::endl;
    }
}

Output: 输出:

IsOk=0
IsOk=0
IsOk2=0
IsOk2=1

!iIsOk although evaluated, does not do anything; !iIsOk尽管已评估,但不执行任何操作; ie removing it will have absolutely no effect on the program. 即删除它对程序绝对没有影响。

Your helpful compiler is warning you of this. 您有用的编译器会警告您这一点。

Did you mean to write !iIsOk as part of the stopping condition in the loop: 您的意思是写!iIsOk作为循环中停止条件的一部分:

iLoop1< iLoopN && iIsOk

or toggle it using 或者使用其切换

iLoop1++, iIsOk = !iIsOk

or the flashier (which personally I find clearer since there are fewer variable repeats) 或闪光点(我个人比较清楚,因为重复次数较少)

iLoop1++, iIsOk ^= true

First, let me tell you the reason behind the warning message. 首先,让我告诉您警告消息背后的原因。 As per the comma operator properties, from C11 , chapter §6.5.17 ( emphasis mine ) 根据逗号运算符的属性,请参阅C11中第6.5.17章( 重点是我的内容

The left operand of a comma operator is evaluated as a void expression; 逗号运算符的左操作数被评估为void表达式; there is a sequence point between its evaluation and that of the right operand. 在它的评估与正确操作数的评估之间存在一个顺序点。 Then the right operand is evaluated; 然后评估正确的操作数; the result has its type and value. 结果具有其类型和价值。

So, here, in your case, the final RHS operand is .... , !iIsOk , for which , the produced value is unused. 因此,在这种情况下,根据您的情况, 最终的 RHS操作数是.... , !iIsOk ,对于它,未使用产生的值。 Hence the warning. 因此,警告。 You need to either make use of the value, or you can get rid of the statement, as in it's current state, it has no effect on the program whatsoever. 您需要使用该值,也可以摆脱该语句,因为它处于当前状态,因此对程序没有任何影响。

That said, as mentioned in the other answer(s) by tilz0R and Bathsheba , per your "specific" requirement, the statement should be iIsOk = !iIsOk but I'd recommend putting this as the last statement in the loop body, for the sake of readability. 就是说,如tilz0RBathsheba在其他答案中所述,根据您的“特定”要求,该语句应为iIsOk = !iIsOk但我建议将其作为循环体内的最后一条语句,出于可读性。

!iIsOk should be iIsOk = !iIsOk . !iIsOk应该是iIsOk = !iIsOk

If C has operator like i++ or ++i , thing you did with !iIsOk does nothing. 如果C具有类似i++++i运算符,那么您对!iIsOk所做的操作!iIsOk不起作用。 If you want result, you have to assing it somewhere. 如果要获得结果,则必须将其附加到某处。

To negate variable, use iIsOk = !iIsOk; 取反变量,请使用iIsOk = !iIsOk; and you will have desired effect. 这样您将获得理想的效果。

暂无
暂无

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

相关问题 警告:逗号的左/右操作数无效 - warning: left-hand/right-hand operand of comma has no effect 逗号的左侧操作数无效与逗号的右侧操作数无效 - Left-hand operand of comma has no effect vs Right-hand operand of comma has no effect 逗号运算符的左和右操作数无效(警告) - Left and Right operand of comma operator has no effect (Warning) 逗号的左手操作数没有效果? - left-hand operand of comma has no effect? 对向量-逗号操作数的左侧无效 - Vector of pairs - left hand side of operand of comma has no effect 使用cin时字符串的右侧操作数错误 - Right-hand operand error on string when using cin C ++没有找到需要右手操作数的&lt;&lt;运算符 - C++ No << operator found which takes right-hand operand 二进制“ [”:找不到使用“初始化列表”类型的右侧操作数的运算符(或者没有可接受的转换) - binary '[': no operator found which takes a right-hand operand of type 'initializer list' (or there is no acceptable conversion) 二进制&#39;operator&#39;:未找到采用&#39;Fraction&#39;类型的右侧操作数的运算符(或没有可接受的转换) - Binary 'operator' : no operator found which takes a right-hand operand of type 'Fraction' (or there is no acceptable conversion) 错误C2679:二进制&#39;==&#39;:未找到采用类型为右侧的操作数的运算符 - error C2679: binary '==' : no operator found which takes a right-hand operand of type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM