首页 > 解决方案 > 使用花括号在 C++ 中初始化变量时出现意外结果

问题描述

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

#include <iostream>

using namespace std;

int main() {

  int myInt {};
  
  return 0;
}

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

dailyPractice10.cpp:7:12: error: expected ';' at end of declaration
int myInt {};
        ^
        ;
1 error generated.

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

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

./dailyPractice10(运行程序)

有没有人有任何反馈,为什么这段代码在 codelite 中运行但不在终端中编译?

标签: c++initializationatom-editorcurly-braces

解决方案


因为这个特性是从 c++11 添加的。

如果您想尝试以下命令。它将起作用。

$ g++ -std=c++0x -o dailyPractice10 dailyPractice10.cpp

推荐阅读