Xcode无法找到'std'命名空间

时间:2014-08-20 19:16:12

标签: c++ ios xcode

我有以下代码

#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
<。>在.h文件中。问题是,当我尝试在xcode中编译它时,它说

#Error: use of undeclared identifier 'std'

在第

struct Thread_Greater : public std::binary_function <Thread,Thread,bool> {

是否有任何我遗失的内容?

1 个答案:

答案 0 :(得分:6)

您需要包含您使用的库组件的标头。在这种情况下,std::binary_function位于<functional>,因此您需要在代码中使用以下行:

#include <functional>

为了解释一下,std命名空间不是内置于C ++语言(主要是)。除非它在程序中的某个地方实际声明,否则就编译器而言它不存在。

甚至可以构建不使用标准库的有用C ++程序。 C ++规范包括甚至可能不包括标准库的模式:独立模式。

如果您使用std命名空间中没有在程序中声明该命名空间的内容,那么您将收到错误消息,告知您std尚未声明。

int main() {
  std::cout << "Hello\n";
}

main.cpp:2:3: error: use of undeclared identifier 'std'
  std::cout << "Hello\n";
  ^

如果您使用了某些内容并且已声明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";
  ~~~~~^