7

I'm trying to compile very simple c++ program by g++ compiler.

//main.cpp 

#include <stdio.h>

using namespace std;

typedef pair<int,int> pii;

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

But I'm getting compilation error: ‘pair’ does not name a type

Compile line: g++ main.cpp -o main.out OS: Ubuntu 16.04 lts g++: gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.2)

If I just add #include<iostrem> program compiles and runs successfully:)

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

using namespace std;

typedef pair<int,int> pii;

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

Do you know, why is this happens?

Wsl_F
  • 832
  • 1
  • 7
  • 16
  • You know what headers are for, do you? – tkausl Oct 16 '16 at 17:53
  • Because, 1. You need to include the right header file, and 2. you need to completely [forget that `using namespace std;` exists](http://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice). – Sam Varshavchik Oct 16 '16 at 17:53
  • 1
    Any C++ standard header is allowed to include any other header, so a random header might do it. The best way is to include the `` header where [`std::pair`](http://en.cppreference.com/w/cpp/utility/pair) is supposed to be declared. – Bo Persson Oct 16 '16 at 17:59

1 Answers1

10

My fault, answer is easy:)

1) For using pair I should include <utility>.

2) <iostream> somewhere includes <utility>, that's why after adding it program compiles successfully:)

Wsl_F
  • 832
  • 1
  • 7
  • 16