0

Using MAP in the below program generate some compiler errors . Not getting what they mean.

   #include <iostream>
   #include <cstdio>
   #include <map>
   #include <cstring>
  using namespace std;

  char maze [61][61], q;
  int n , m , i , j , x , y;
  map < char , char > left ;
  map < char , char > right ;
  char orient ;

   int main(){

   left ['N'] = 'W' ;
   left ['E'] = 'S';
   left['S'] = 'E';
   left['W'] = 'N';

   right['N'] = 'E';
   right['E'] = 'S';
   right['S'] = 'W';
   right['W'] = 'N';

     scanf( "%d %d" , &n , &m) ;

    for ( i = 0 ; i < n ; i++)
    scanf("%s", maze[i]);

    scanf("%d %d" , &x ,&y);
    orient = 'N' ;
    x = x - 1 ; y = y - 1 ;
    return 0 ;
   }

Getting compiltaion errors like :

     prog.cpp: In function ‘int main()’:
     prog.cpp:15:1: error: reference to ‘left’ is ambiguous
                    left['N'] = 'W';

     prog.cpp:9:21: note: candidates are: std::map<char, char> left
                           map < char , char > left ;
            In file included from /usr/include/c++/4.8/ios:42:0,
             from /usr/include/c++/4.8/ostream:38,
             from /usr/include/c++/4.8/iostream:39,
             from prog.cpp:1:   /usr/include/c++/4.8/bits/ios_base.h:916:3:
              note:      std::ios_base& std::left(std::ios_base&)
              left(ios_base& __base)

What is the mistake can you please point out and what does it mean ?? For more details :
http://ideone.com/CqBiS0

Amit Kumar
  • 1,477
  • 1
  • 16
  • 27

3 Answers3

9

Functions with name left and right already exists in ios header. They are IO manipulators.

So, this is why you should use explicit namespace resolution instead of just writing using namespace std. And, of course, you should avoid using global variables.

awesoon
  • 32,469
  • 11
  • 74
  • 99
  • 1
    Or move variables to main(), std namespace is rather useful to have. – PTwr Dec 31 '13 at 17:31
  • In fact, `using namespace std;` can be dangerous. See http://stackoverflow.com/a/1453605/10077 – Fred Larson Dec 31 '13 at 17:33
  • Why not just avoid reserved names like left, right, map, etc... in your own functions. using `using namespace std` ensures that you do not end up with such silly names in your own functions. That would be like naming your own function printf or something. – Brandin Dec 31 '13 at 19:35
3

Here's your culprit:

using namespace std;

This drags the standard library's std::left into the global namespace, where it conflicts with your own left in the same namespace. Get rid of it, and add std:: to any names from the standard library you're using. Dumping the entirety of a large namespace into the global namespace is a recipe for name conflicts like this one.

Also, you probably don't want to use global variables; they are fraught with more perils than just namespace pollution. Moving the variables inside main will also fix this particular error. I suggest you do both, and make both habits.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
3

In the iostream header, there's a symbol named left. Luckily, it's under the std namespace. Unfortunately, you are using namespace std, so both your map and the symbol from the iostream header are now referenced by "left" - which creates this ambiguity. To resolve this, either: 1. Rename your map, or 2. Remove the "using namespace std" statement, and use the prefix std:: where it's required.

PaF
  • 3,297
  • 1
  • 14
  • 15