编译
CLI 编译 xx.cpp
/// g++ 编译
1. 编译 > g++ -o out xx.cpp
2. 运行 > ./out
identifier "count" is undefined
报错
- You need to either use the std:: prefix
- add using namespace std; at the beginning of the file
/// 正确
#include <iostream>
using namespace std;
namespace Zidane
{
class Zidane
{
public:
void zidane()
{
cout << "Zidane" << endl;
}
};
}
gcc 编译
gcc 编译需要链接 c++标准库。
因为 gcc 是编译c代码,不会移动链接c++标准库。
> gcc -x c++ -lstdc++ -o out helloword.cpp
vs code 运行 c++
- 安装 code runner 插件
- 右上角 run code
- output 中查看输出
CLI编译使用c++11特性
问题
问题:helloworld.cpp:19:16: warning: 'auto' type specifier is a C++11 extension [-Wc++11-extensions]
解决
VS Code setting 搜 code-runner.executorMap,在 c++ 编译参入加上
-std=c++11
修改:setting.json
容器类
容器类
命名空间
using 指令有如下两种形式:
- using 命名空间::成员名;:这条指令可以让我们省略某个成员名前的命名空间,直接通过成员名访问成员,相当于将这个成员导入了当前的作用域。
- using namespace 命名空间;:这条指令可以直接通过成员名访问命名空间中的 任何 成员,相当于将这个命名空间的所有成员导入了当前的作用域。
⚠️ using 指令可能会导致命名冲突!
由于 using namespace std; 会将 std 中的 所有名字 引入,因此如果声明了与 std 重名的变量或函数,就可能会因为命名冲突而导致编译错误。
因此在工程中,并不推荐使用 using namespace 命名空间; 的指令。