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
25.5k12 gold badges83 silver badges95 bronze badges
asked Apr 29, 2011 at 21:06

2 Answers 2

5

map is in the std namespace. Try

bool delete_lowest(std::map<char, double> &frequencies)
answered Apr 29, 2011 at 21:08

Comments

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.
answered Apr 29, 2011 at 21:13

2 Comments

If it's in a header file, there's really only one option. stackoverflow.com/questions/4872373/…
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.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.