473,387 Members | 1,572 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Template as a base class.

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)

Prefixing begin() with this->begin() or std::list<std::pair<K, V>
>::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::pair< K, V >& pair) const
{
return _M_value == pair.first;
}

private:
K _M_value;
};

template < typename K, typename V >
class Assoc : public std::list< std::pair< K, V
{
typedef typename std::list< std::pair< 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 ();
}
};

Aug 26 '06 #1
3 2363
Kyku wrote:
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)

Prefixing begin() with this->begin() or std::list<std::pair<K, V>

::begin() solves the problem. But I wonder why it can't look for the function in the base class.
Section 14.6.2 §3 of the 2003 revision of the standard states: "... the
base class scope is not examined during unqualified name lookup either
at the point of definition of the class template or member or during an
instantiation of the class template or member."

This is a clarification of the original wording that was ambiguous.

--
Ian Collins.
Aug 26 '06 #2

"Kyku" <kw******@gmail.comskrev i meddelandet
news:11********************@75g2000cwc.googlegroup s.com...
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)

Prefixing begin() with this->begin() or std::list<std::pair<K, V>
>>::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
There could be specializations of the base classes that not all
contain a begin() function. To check this, the compiler could have to
instantiate the base class for all combinations of K and V.

Also, if there is also a namespace scope begin() function available,
should it choose that one when it is not present in the base class?
Not a good idea!
Bo Persson
Aug 27 '06 #3
Bo Persson wrote:
There could be specializations of the base classes that not all
contain a begin() function. To check this, the compiler could have to
instantiate the base class for all combinations of K and V.
And, during parsing, it needs to know what's a type or a template,
because C++'grammer is screwed.

If C++'s grammer was slightly more sane, none of this would be necessary.

Jens
Aug 27 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Massimiliano Alberti | last post by:
Can I specialize a template function in a subclass without overriding it? (the main template function is defined in a base class). Now I'm doing something like that: (in base class)...
13
by: Walt Karas | last post by:
The following gives an error in the declaration of the member function x() of the class template Tpl, compiliing with a recent version of GCC under Solaris: class A { }; class B { }; ...
2
by: Tony Johansson | last post by:
Hello Experts To derive a concrete class from a template class in invalid. Why?? So you should not be able have something like this class Derived : public Base<int, 100> It's valid to derive...
3
by: Chris | last post by:
I am having a very strange problem involving virtual functions in template classes. First of all, here is an extremely simplified structure of the two classes I am having problems with. ...
11
by: Niels Dekker - no reply address | last post by:
The following attempt to pass my template "Base" as a template template argument was rejected by Microsoft VC++ 8.0 (2005), while it still works on VC++ 7.1 (2003). Is it correct C++? And is...
8
by: Imre | last post by:
Hi I'm looking for a way to make sure that whenever a new instance of a class template A is created, then an instance of class template B is also created, with the same template parameters. Of...
3
by: IR | last post by:
Hi, I've been trying to do the following (which doesn't compile) : template<class T, class F = Example<T struct Example { F foo(); };
4
by: Grizlyk | last post by:
Hello. Why were base class "typedefs" hidden by template<and explicit usage of them does not work too? Try open only one of the lines in the example below //using Tparent::Tptr; //typedef...
12
by: stefan.bruckner | last post by:
Hi, I am looking for a way to achieve the following. I've tried a couple of things, but they all ended up being too complicated: I have a templated class A. I want another class B to be able...
2
by: syang8 | last post by:
Dear all, I am trying to design classes with stream support. Basically, I want the operator << work for the base class and all the derived classes. If the base class is a template class, and...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.