gpt4 book ai didi

c++ - 为什么此代码会产生引用逗号运算符的警告?

转载 作者:可可西里 更新时间:2023-11-01 17:52:32 25 4
gpt4 key购买 nike

回答this question时,我遇到了这段代码......

#include <iostream>

int main()
{
int const income = 0;
std::cout << "I'm sorry your income is: " < income; // this is line 6
}

...其中包含错字。第二个(预定)<<第 6 行的运算符被意外写成了 < .

除此之外,编译代码 using GCC 4.3.4或 4.4.3 导致警告:

prog.cpp: In function ‘int main()’:
prog.cpp:6: warning: right-hand operand of comma has no effect

我的问题:为什么会产生那个特定的警告?它指的是哪个逗号运算符?

注意:我并不是提倡故意使用单个 <cout声明。 我只是在试图找出我链接到的另一个问题的答案时偶然发现了这个警告,并且很好奇为什么编译器会生成它。

最佳答案

我想他们只是忘了更改警告文本

int main() {
1, 2;
}

prog.cpp:2: warning: left-hand operand of comma has no effect
prog.cpp:2: warning: right-hand operand of comma has no effect

expr, expr 运算符计算左操作数,然后计算右操作数并产生右操作数的计算结果。如果正确的操作数没有效果并且没有使用它的值,则可能是程序中的错误。

现在他们似乎只是滥用上面的警告文本来警告其他二元运算符。

关于c++ - 为什么此代码会产生引用逗号运算符的警告?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5665611/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com