`没有使用`namespace std;`和文件命名类型`错误

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会对此产生影响吗?我可能会遗漏的其他东西?

cni*_*tar 7

是的,但在这个例子functions.cpp中没有看到,using namespace std因为你只写了main.cpp.


不添加using namespace stdfunctions.h,使用std::合格的类型.添加a using..会对标头的用户造成不必要的负担.


jua*_*nza 7

您需要限定命名空间:

#include "functions.h"
std::vector<int> x;
Run Code Online (Sandbox Code Playgroud)

你有一个using namespace stdmain.cpp,而且它不能被看出functions.cpp.这是问题的根源.

一般来说,你应该避免using namespace std,特别是在标题中.如果你真的必须main把它包括在内,那就把它放在所有标题之后.