C++编译错误:“pair”未命名类型

C++ compilation error: ‘pair’ does not name a type

本文关键字:未命名 类型 pair 编译 错误 C++      更新时间:2023-10-16

我正试图用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 ltsg++:gcc版本5.4.0 20160609(Ubuntu 5.4.0-6ubuntu1~16.04.2(

如果我只是添加#include<iostrem>程序编译并成功运行:(

#include <stdio.h>
#include<iostream>
using namespace std;
typedef pair<int,int> pii;
int main(int argc, char *argv[])
{
    printf("Hi");
    return 0;
}

你知道为什么会这样吗?

我的错,答案很简单:(

1( 对于使用pair,我应该包括<utility>

2( <iostream>某处包含<utility>,这就是为什么添加它后程序成功编译的原因:(