我正在使用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++11开始添加的。
如果你想尝试下面的命令,它会起作用。
$ g++ -std=c++0x -o dailyPractice10 dailyPractice10.cpp
关于c++ - 使用花括号在 C++ 中初始化变量时出现意外结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62488270/