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
map
is in the std
namespace. Try
bool delete_lowest(std::map<char, double> &frequencies)
answered Apr 29, 2011 at 21:08
Comments
There are three solutions to your error:
- Instead of
map
usestd::map
- add
using std::map
before your class - add
using namespace std
before your class.
answered Apr 29, 2011 at 21:13
2 Comments
Cubbi
If it's in a header file, there's really only one option. stackoverflow.com/questions/4872373/…
Tilman Vogel
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.lang-cpp