c++ - 使用花括号在 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++11 添加的。
如果您想尝试以下命令。它将起作用。
$ g++ -std=c++0x -o dailyPractice10 dailyPractice10.cpp
推荐阅读
- python - IronPython 脚本使用 C# Reference throws ArgumentTypeException expected got the same value
- spring-boot - 升级 Spring Boot 版本时不存在主题替代名称
- android - 如果文件存在于内部存储中,则为文件名添加后缀
- javascript - 替换对象数组中属性的最佳方法
- json - 如何将字符串中带有双引号的json文件加载到spark scala中的数据框中
- docker-compose - /bin/sh: url: 找不到命令
- azure-ad-b2c - ADB2C 中单个租户的不同自定义策略的不同样式
- android - onBackPressed 时活动-webview 之间的点击次数
- typescript - 我可以以编程方式将字符串数字文字类型转换为其数字对应物吗?(打字稿)
- python - ImportError:无法从“tensorflow.python.eager.context”导入名称“get_config”[起点:从 keras.layers.core 导入激活]