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".

1 Answer 1

12

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Just replace your string with "top" with what Neil suggests.
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 - that's why I refer them to the faq :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.