C++ 使用`namespace std;` 和文件`不命名类型` 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12158223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
`does not name a type` error with `namespace std;` and files
提问by Matt Munson
compiling the code below with g++ main.cpp functions.cpp -o run
gives me the error error: ‘vector' does not name a type
. Declaring namespace at the top of main.cpp
usually works across all .cpp
files for me.
编译下面的代码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 () {}
functions.h
函数.h
#include <vector>
functions.cpp
函数.cpp
#include "functions.h"
vector <int> x;
EDIT: I appreciate the fact all responders know what their talking about, but this normally works for me. Would the use of a makefile have any bearing on that? something else that I might be missing?
编辑:我很感激所有响应者都知道他们在说什么,但这通常对我有用。使用 makefile 会对此有任何影响吗?我可能会错过的其他东西?
回答by cnicutar
Yes but in this example functions.cpp
has not seen using namespace std
since you only wrote that in main.cpp
.
是的,但在这个例子functions.cpp
中没有看到,using namespace std
因为你只在main.cpp
.
Don't add using namespace std
to functions.h
, use std::
to qualify types. Adding a using..
imposes an unnecessary burden on the user of your header.
不要添加using namespace std
到functions.h
,用于std::
限定类型。添加 ausing..
会给标题的用户带来不必要的负担。
回答by juanchopanza
You need to qualify the namespace:
您需要限定命名空间:
#include "functions.h"
std::vector<int> x;
You have a using namespace std
in main.cpp
, and it cannot be seen by functions.cpp
. That is the root of the problem.
你有一个using namespace std
in main.cpp
,它不能被 看到functions.cpp
。这就是问题的根源。
In general, you should avoid using namespace std
, specially in headers. And if you really must include it in main
, put it after all the headers.
一般来说,你应该避免using namespace std
,特别是在标题中。如果您确实必须将其包含在 中main
,请将其放在所有标题之后。
回答by Luchian Grigore
You imported the std
namespace only in main.cpp
, not in functions.cpp
.
您std
仅在 中导入了命名空间main.cpp
,而不是在functions.cpp
.
You have to qualify your use - std::vector
in the second file, or use the using
directive:
您必须限定您的使用 -std::vector
在第二个文件中,或使用using
指令:
//functions.cpp
#include "functions.h"
std::vector <int> x; // preferred
or
或者
//functions.cpp
#include "functions.h"
using namespace std;
vector <int> x;
or (bonus)
或(奖金)
//functions.cpp
#include "functions.h"
using std::vector;
vector <int> x;
Declaring namespace at the top of main.cpp usually works across all .cpp files for me.
在 main.cpp 顶部声明命名空间通常适用于我的所有 .cpp 文件。
You have a really flawed compiler then. using
directives shouldn't influence translation units that don't have direct visibility over the directive.
那么你的编译器真的有缺陷。using
指令不应影响对指令没有直接可见性的翻译单元。
回答by mathematician1975
You using namespace std
is local to main.cpp only. You need to use
您using namespace std
仅在 main.cpp 本地。你需要使用
std::vector<int> x;
in your source file functions.cpp
在你的源文件functions.cpp中