使用花括号在 C++ 中初始化变量时出现意外结果

A.V*_*.S. 2 c++ initialization curly-braces atom-editor

我正在使用atom 来练习C++(我很新)。我刚刚学会了初始化变量,如下所示:

#include <iostream>

using namespace std;

int main() {

  int myInt {};
  
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

当我在 Codelite 中构建并运行之前的代码时,我没有收到任何错误。但是,如果我使用 MacBook 终端 (zsh) 编译 Atom 文件 dailyPractice10.cpp,则会出现以下错误:

dailyPractice10.cpp:7:12: error: expected ';' at end of declaration
int myInt {};
        ^
        ;
1 error generated.
Run Code Online (Sandbox Code Playgroud)

我使用以下命令在终端上编译它:

g++ -o dailyPractice10 dailyPractice10.cpp (编译)

./dailyPractice10(运行程序)

有谁有任何反馈为什么此代码在 codelite 中运行但不在终端中编译?

Nil*_*nki 5

因为这个功能是从c++11开始添加的。

如果你想尝试下面的命令,它会起作用。

$ g++ -std=c++0x -o dailyPractice10 dailyPractice10.cpp
Run Code Online (Sandbox Code Playgroud)