K
Kyku
Hello, I'm having a problem with the following code. G++ complains
that:
assoc.cpp: In member function 'V& Assoc<K, V>::ValueOf(const K&)':
assoc.cpp:35: error: there are no arguments to 'begin' that depend on a
template parameter, so a declaration of 'begin' must be available
assoc.cpp:35: error: (if you use '-fpermissive', G++ will accept your
code, but allowing the use of an undeclared name is deprecated)
Please explain.
Thanks in advance, Kyku
#include <list>
#include <utility>
#include <algorithm>
struct KeyNotFound {};
// This is to compare a key given in the constructor to the key in a
pair.
template < typename K , typename V >
struct KeyEqual
{
public:
KeyEqual (const K& key) : _M_value (key) {}
bool operator () (const std:air< K, V >& pair) const
{
return _M_value == pair.first;
}
private:
K _M_value;
};
template < typename K, typename V >
class Assoc : public std::list< std:air< K, V > >
{
typedef typename std::list< std:air< K, V > >::iterator iterator;
public:
void Insert (const K& key, const V& val) throw (std::bad_alloc)
{
push_back (make_pair (key, val));
}
V& ValueOf (const K& key) throw (KeyNotFound)
{
iterator iter;
iter = std::find_if (begin (), this->end (), KeyEqual< K, V >
(key)); // <<- HERE IS THE PROBLEM
if (iter == this->end ()) throw KeyNotFound ();
return iter->second;
}
bool Exists (const K& key) throw ()
{
return std::find (this->begin (), this->end (), key) !=
this->end ();
}
};
that:
assoc.cpp: In member function 'V& Assoc<K, V>::ValueOf(const K&)':
assoc.cpp:35: error: there are no arguments to 'begin' that depend on a
template parameter, so a declaration of 'begin' must be available
assoc.cpp:35: error: (if you use '-fpermissive', G++ will accept your
code, but allowing the use of an undeclared name is deprecated)
Prefixing begin() with this->begin() or std::list said:::begin() solves the problem. But I wonder why it can't look for the function in the base class.
Please explain.
Thanks in advance, Kyku
#include <list>
#include <utility>
#include <algorithm>
struct KeyNotFound {};
// This is to compare a key given in the constructor to the key in a
pair.
template < typename K , typename V >
struct KeyEqual
{
public:
KeyEqual (const K& key) : _M_value (key) {}
bool operator () (const std:air< K, V >& pair) const
{
return _M_value == pair.first;
}
private:
K _M_value;
};
template < typename K, typename V >
class Assoc : public std::list< std:air< K, V > >
{
typedef typename std::list< std:air< K, V > >::iterator iterator;
public:
void Insert (const K& key, const V& val) throw (std::bad_alloc)
{
push_back (make_pair (key, val));
}
V& ValueOf (const K& key) throw (KeyNotFound)
{
iterator iter;
iter = std::find_if (begin (), this->end (), KeyEqual< K, V >
(key)); // <<- HERE IS THE PROBLEM
if (iter == this->end ()) throw KeyNotFound ();
return iter->second;
}
bool Exists (const K& key) throw ()
{
return std::find (this->begin (), this->end (), key) !=
this->end ();
}
};