【问题标题】:C++ compilation error: ‘pair’ does not name a typeC++ 编译错误:‘pair’没有命名类型
【发布时间】:2016-10-16 17:50:42
【问题描述】:

我正在尝试通过 g++ 编译器编译非常简单的 c++ 程序。

//main.cpp 

#include <stdio.h>

using namespace std;

typedef pair<int,int> pii;

int main(int argc, char *argv[])
{
    printf("Hi");
    return 0;
}

但我收到编译错误:“pair”没有命名类型

编译行:g++ main.cpp -o main.out 操作系统:Ubuntu 16.04 lts g++:gcc版本5.4.0 20160609(Ubuntu 5.4.0-6ubuntu1~16.04.2)

如果我只是添加#include&lt;iostrem&gt;程序编译运行成功:)

#include <stdio.h>
#include<iostream>

using namespace std;

typedef pair<int,int> pii;

int main(int argc, char *argv[])
{
    printf("Hi");
    return 0;
}

你知道,为什么会这样吗?

【问题讨论】:

  • 你知道标题的用途吗?
  • 因为,1.需要包含正确的头文件,2.需要完全forget that using namespace std; exists
  • 任何 C++ 标准标头都允许包含任何其他标头,因此随机标头可能会这样做。最好的方法是在应该声明 std::pair 的地方包含 &lt;utility&gt; 标头。

标签: c++ g++ ubuntu-16.04


【解决方案1】:

我的错,答案很简单:)

1) 对于使用pair,我应该包括&lt;utility&gt;

2) &lt;iostream&gt; 某处包含&lt;utility&gt;,这就是为什么添加它后程序编译成功:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-01-05
    • 2021-09-21
    • 2015-12-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-01
    • 1970-01-01
    相关资源
    最近更新 更多