【问题标题】:C++ comma operatorC++ 逗号运算符
【发布时间】:2017-07-11 04:29:39
【问题描述】:

我正在尝试从 C++ Primer plus 运行此代码

#include <iostream>
using namespace std;

int main() {
    int i = 20, j= 2*i;
    cout << "i = " << i << endl;
    int cats = 17,240;  //No, I don't want the number 17240
    return 0;
}

为什么我看到这个错误 expected unqualified-id before numeric constant int cats = 17,240; ,我不知道,我需要一个简短的解释。谢谢

【问题讨论】:

  • 只需删除,int cats = 17240;
  • 你到底想用 17,240 做什么?

标签: c++11 comma-operator


【解决方案1】:
由于运算符优先级

int cats = 17,240; 将被编译器视为int (cats = 17),240;。而int 240; 毫无意义,因此会发出编译器诊断。

你想要17240 猫吗?如果是这样,请删除逗号。

【讨论】:

  • 请查看预订链接。我只是想完全按照书中“逗号运算符花絮”标题下写的行(链接在问题中)。
  • @DeepChandra:不要在问题中包含这样的链接。它们可能很脆弱,您的特定的在某些司法管辖区无效。
猜你喜欢
  • 2010-12-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-04
  • 1970-01-01
  • 2016-10-28
  • 2018-02-20
  • 1970-01-01
相关资源
最近更新 更多