Mat*_*son -1 c++ namespaces header
编译下面的代码g++ main.cpp functions.cpp -o run给我错误error: ‘vector’ does not name a type.在顶部声明命名空间main.cpp通常适用于所有.cpp文件.
main.cpp中
using namespace std;
#include "functions.h"
main () {}
Run Code Online (Sandbox Code Playgroud)
functions.h
#include <vector>
Run Code Online (Sandbox Code Playgroud)
functions.cpp
#include "functions.h"
vector <int> x;
Run Code Online (Sandbox Code Playgroud)
编辑:我很欣赏所有响应者都知道他们在谈论什么,但这通常对我有用.使用makefile会对此产生影响吗?我可能会遗漏的其他东西?
是的,但在这个例子functions.cpp中没有看到,using namespace std因为你只写了main.cpp.
不添加using namespace std到functions.h,使用std::合格的类型.添加a using..会对标头的用户造成不必要的负担.
您需要限定命名空间:
#include "functions.h"
std::vector<int> x;
Run Code Online (Sandbox Code Playgroud)
你有一个using namespace std在main.cpp,而且它不能被看出functions.cpp.这是问题的根源.
一般来说,你应该避免using namespace std,特别是在标题中.如果你真的必须main把它包括在内,那就把它放在所有标题之后.
| 归档时间: |
|
| 查看次数: |
22812 次 |
| 最近记录: |