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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 15:56:54  来源:igfitidea点击:

`does not name a type` error with `namespace std;` and files

c++headernamespaces

提问by Matt Munson

compiling the code below with g++ main.cpp functions.cpp -o rungives me the error error: ‘vector' does not name a type. Declaring namespace at the top of main.cppusually works across all .cppfiles 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.cpphas not seen using namespace stdsince you only wrote that in main.cpp.

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



Don't add using namespace stdto functions.h, use std::to qualify types. Adding a using..imposes an unnecessary burden on the user of your header.

不要添加using namespace stdfunctions.h,用于std::限定类型。添加 ausing..会给标题的用户带来不必要的负担。

回答by juanchopanza

You need to qualify the namespace:

您需要限定命名空间:

#include "functions.h"
std::vector<int> x;

You have a using namespace stdin main.cpp, and it cannot be seen by functions.cpp. That is the root of the problem.

你有一个using namespace stdin 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 stdnamespace only in main.cpp, not in functions.cpp.

std仅在 中导入了命名空间main.cpp,而不是在functions.cpp.

You have to qualify your use - std::vectorin the second file, or use the usingdirective:

您必须限定您的使用 -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. usingdirectives shouldn't influence translation units that don't have direct visibility over the directive.

那么你的编译器真的有缺陷。using指令不应影响对指令没有直接可见性的翻译单元。

回答by mathematician1975

You using namespace stdis 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中