By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,851 Members | 2,097 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,851 IT Pros & Developers. It's quick & easy.

Can't make sense of a template.

P: n/a
I set out to make a templated priority queue class, based on
std::vector. I didn't get very far at all before my attempts got cut
short by an error on the following line (see below for context):

std::vector<T>::iterator i ;

My compiler (g++ 2.96, as well as g++ 3.0.4) produce the error: parse
error before `;'

I do not understand why I cannot instantiate an iterator like this.
Replacing 'T' with 'int', 'double' or even something strange like
'std::vector<int> ' works just fine.

Below is the full code, reduced to the point necessary to demonstrate
the error (assume that T is a suitable type to be displayed by cout).

Thanks,
Alan
template <class T>
class pqueue
{
private :
std::vector<T> heap ;
public :

void dump_heap()
{
std::vector<T>::iterator i ;

for (i = heap.begin(); i != heap.end(); i++)
std::cout << *i << std::endl ;
}
} ;
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Alan Johnson <al****@mailandnews.com> wrote in news:40a00309$1
@news.ua.edu:
I set out to make a templated priority queue class, based on
std::vector. I didn't get very far at all before my attempts got cut
short by an error on the following line (see below for context):
(...)

template <class T>
class pqueue
{
private :
std::vector<T> heap ;
public :

void dump_heap()
{
You should use the 'typename' keyword like this:

typename std::vector<T>::iterator i ;
for (i = heap.begin(); i != heap.end(); i++)
std::cout << *i << std::endl ;
}
} ;


The 'typename' keyword is required within template definitions. It is
used as a necessary hint for the compiler, which explicitly indicates
that given symbol is actually a type.

--
:: bartekd [at] o2 [dot] pl

Jul 22 '05 #2

P: n/a
On Mon, 10 May 2004 17:38:11 -0500, Alan Johnson <al****@mailandnews.com> wrote:
I set out to make a templated priority queue class, based on
std::vector. I didn't get very far at all before my attempts got cut
short by an error on the following line (see below for context):

std::vector<T>::iterator i ;
The compiler doesn't know that std::vector<T>::iterator is a type and not
say a variable. In fact, what it is could cary with T.

typename std::vector<T>::iterator i ;

Tells the compiler that std::vector<T>::iterator is the name of a type.

My compiler (g++ 2.96, as well as g++ 3.0.4) produce the error: parse
error before `;'
I have earlier and later versions which give different results.

2.95 compiles it just fine.
3.2 and 3.3 give warnings about "implicit typename is deprecated".

I do not understand why I cannot instantiate an iterator like this.
Replacing 'T' with 'int', 'double' or even something strange like
'std::vector<int> ' works just fine.


Replacing T like that means the compiler then knows that iterator is a
type, since it can check the template definitions (e.g. the possibility
of a particular T giving a template specialisation in which iterator is
not a type is removed).

--
Sam Holden
Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.