473,405 Members | 2,373 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,405 software developers and data experts.

Can't make sense of a template.

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
2 1078
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ben | last post by:
Hi. I am currently trying to move compilers (VC6 -> VC7 in fact) and I'm having a few problems (so far exclusively to do with better standard conformance). Anyhow, whilst rebuilding a...
6
by: vijay | last post by:
Hello I wanted to understand a contradictory design of C++ class A {public: virtual void f(){ cout<<" base f"<<endl; } }; class B:public A {
1
by: Marco Leist | last post by:
hello, iŽd like to replace certain strings in a XML-Document with other ones. Thereby the structure (tags)of the source-xml should be preserved completly, the strings that should be replaced are...
8
by: William Payne | last post by:
As the title says, is something like this legal: template<typename T> class foo { public: static T* t; }; T* foo::t;
22
by: Ian | last post by:
The title says it all. I can see the case where a function is to be called directly from C, the name mangling will stuff this up. But I can't see a reason why a template function can't be...
4
by: skatemore9690 | last post by:
#1 in Calculus you learned that log(1+x) = x - x^2/2 + x^3/3 - x^4/4 + ... for x in the interval (-1,1] (here x^2 means "x squared", etc.). Write a program which asks the user to type a...
31
by: Sarita | last post by:
Hello, this might sound stupid, but I got a really nice homepage template which unfortunately is a 3-Column Fixed Width CSS format. Now I don't have any content for the right column and would...
17
by: Juha Nieminen | last post by:
As we know, the keyword "inline" is a bit misleading because its meaning has changed in practice. In most modern compilers it has completely lost its meaning of "a hint for the compiler to inline...
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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.