Can't make sense of a template.

A

Alan Johnson

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 ;
}
} ;
 
B

bartek

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 said:
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.
 
S

Sam Holden

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 said:
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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top