Xcode 找不到“std”命名空间
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25412723/
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
Xcode cannot find 'std' namespace
提问by Jean-Luc Nacif Coelho
I have the following code
我有以下代码
#ifndef TPSO1_thread_h
#define TPSO1_thread_h
#define _XOPEN_SOURCE
#include <ucontext.h>
struct Thread_Greater;
class Thread {
...
friend struct Thread_Greater;
friend class Scheduler;
};
struct Thread_Greater : public std::binary_function <Thread,Thread,bool> {
...
};
#endif
in a .h file. Problem is, when I try to compile it in xcode it says
在 .h 文件中。问题是,当我尝试在 xcode 中编译它时,它说
#Error: use of undeclared identifier 'std'
In the line
在行
struct Thread_Greater : public std::binary_function <Thread,Thread,bool> {
Is there any include that I am missing?
有没有我遗漏的包括?
回答by bames53
You need to include the headers for the library components you use. In this case std::binary_functionis in <functional>, so you need this line in your code:
您需要包含您使用的库组件的标头。在这种情况下std::binary_function是 in <functional>,因此您的代码中需要这一行:
#include <functional>
Just to explain a bit more, the stdnamespace is not built-in to the C++ language (mostly). Unless it's actually declared somewhere in a program then it does not exist as far as the compiler is concerned.
只是为了多解释一点,std命名空间不是 C++ 语言(大部分)内置的。除非它实际上在程序中的某处声明,否则就编译器而言它不存在。
It's even possible to build useful C++ programs that make no use of the standard library. The C++ specification includes a mode that may not even include a standard library: freestanding mode.
甚至可以构建不使用标准库的有用 C++ 程序。C++ 规范包括一种甚至可能不包括标准库的模式:独立模式。
If you use something from the stdnamespace without that namespace having been declared in the program then you'll get an error telling you stdhasn't been declared.
如果您使用std命名空间中的某些内容而没有在程序中声明该命名空间,那么您将收到一条错误消息,告诉您std尚未声明。
int main() {
std::cout << "Hello\n";
}
main.cpp:2:3: error: use of undeclared identifier 'std'
std::cout << "Hello\n";
^
If you use something and stdhas been declared, but not the specific member of stdyou're using, then you'll get an error about stdnot containing the thing you're using:
如果你使用了一些东西并且std已经被声明,但不是std你正在使用的特定成员,那么你会得到一个关于std不包含你正在使用的东西的错误:
#include <utility> // declares std, but not std::cout
int main() {
std::cout << "Hello\n";
}
main.cpp:4:12: error: no member named 'cout' in namespace 'std'
std::cout << "Hello\n";
~~~~~^

