I’m trying to make a wrapper to the STL map container, in order to add a const method to return the value given the key. In map, operator[] isn’t const, and find() requires dereferencing to get the value (map.find()->second). I’m basing some of my “research” off of Idiomatic C++ for reading from a const map by the way.
The code so far (all inside a single header file):
#include <map>
template <typename K, typename V>
class easymap : public std::map<K, V>
{
//Constructor
easymap() : std::map<K, V>() {};
//The get method
V get(K key)
{
std::map<K, V>::const_iterator iter(find(key));
return iter != end() ? iter->second : V();
}
};
When I try to compile this, I get the following errors:
In member function `V easymap::get(K)':
expected `;' before "iter"
`iter' was not declared in this scope
there are no arguments to `end' that depend on a template parameter, so a declaration of `end' must be available|
(if you use `-fpermissive', G++ will accept your code, but allowing the use of an undeclared name is deprecated)
Does how I’m trying to go about doing this make sense? If so, how do I make this work? If not, how would I go about achieving the effect I’m looking for?
You are missing the template parameters for map, you have to specify
typenamewhen declaring the iterator (see here), and for some reason unknown to me (probably a namespace conflict) you have to usethiswhen callingend():