0

I can't make map work with a class, what's wrong here? I can't figure it out, please help:

#include <map>
#include <iterator>

class base {
public:

    bool delete_lowest(map<char, double> &frequencies)
    {
        double min=1; char del ; box b1;
        for (iterator itr = frequencies.begin(); itr != frequencies.end(); ++itr)
        {
            if(itr->second < min)
            {
                min= itr->second ;
                del= itr->first ;
            }
        }
        frequencies.erase(del) ;
        return true;
    }

I am getting errors like "map is not declared" and so on. I think the way I code is not the proper way. so how do I proceed? Thanks

Marc Mutz - mmutz
  • 24,485
  • 12
  • 80
  • 90
user721776
  • 33
  • 1
  • 3

2 Answers2

5

map is in the std namespace. Try

bool delete_lowest(std::map<char, double> &frequencies)
dfan
  • 5,714
  • 1
  • 31
  • 27
2

There are three solutions to your error:

  1. Instead of map use std::map
  2. add using std::map before your class
  3. add using namespace std before your class.
Afiefh
  • 920
  • 1
  • 7
  • 15
  • 2
    If it's in a header file, there's really only one option. http://stackoverflow.com/questions/4872373/why-is-including-using-namespace-into-a-header-file-a-bad-idea-in-c – Cubbi Apr 29 '11 at 21:16
  • 2
    If the class is declared in a header file, better do not use either (2) or (3): It imports `std::map` or the whole `std` namespace into all modules that include that header. This kind of namespace pollution is often undesirable. So, in the header, use explicit qualification, in the implementation file, you can still freely use (2) and (3). Or, you could even put a `typedef std::map map;` into your class. – Tilman Vogel Apr 29 '11 at 21:17