tags:

views:

160

answers:

1
template<class T>
    class mStack
    {
    private:
       vector<T> a; 
       vector<T>::iterator top;
    public:
       void push(T);
       T pop();
       mStack();
       void printStack();
};

The code with above class is not getting compiled... why? What is the problem? The compiler says "expected ; above top".

+12  A: 

You need a typename:

typename vector<T>::iterator top;

This reassures the compiler thar vector<T> really is a type. For a discussion of this and other template gotchas, see the C++ FAQ.

anon
I can';t get it.. can you show me some example?
suresh
Just replace your string with "top" with what Neil suggests.
sharptooth
I wish that I could write a good, simple explanation for when exactly it's required, but in reality most of the time just adding typename before anything which is a type which uses a template parameter is harmless and works.
Pete Kirkham
It shows some error
suresh
can u make change and post?
suresh
@pete - that's why I refer them to the faq :-)
anon
Thank you very much guys... I got it .
suresh