I'm writing a template class as below. Trying to add a funtion to browse all the elements of an instance, that needs a vector iterator.

When I compile the code getting a list of errors. Two of them typically draw my attention.
The Error reads

error: dependent-name ` std::vector<T,std::allocator<_CharT> >::iterator' is parsed as a non-type, but instantiation yields a type
3_stack_with_exception_handling.cpp:27: note: say `typename std::vector<T,std::allocator<_CharT> >::iterator' if a type is meant
The code is as below.
Can sombody fix the error please?

using namespace std;

template<typename T>
class myStack {
                vector <T> elems;
                void push(T const &);
                //void pop();
                T pop();

                T top() const;
                bool empty() const
                        return elems.empty();
                void clear();
                //void browse();
                //void myStack<T> :: browse()
                void browse()
                        //template<typename T>
                        vector<T> :: iterator ptr;
                         ptr = elems.begin();
                          //while(elems.end() != true){
                               cout << *ptr << endl;
                                cout << *ptr << endl;

template<typename T>
void myStack<T> :: clear()

template<typename T>
void myStack<T> :: push(T const &ob)
        cout << "TRACE: push()" << endl;
template <typename T>
void myStack <T> :: pop()
template <typename T>
T myStack <T> :: pop()
        cout << "TRACE: pop() " << endl;
        if(elems.empty()) {
                throw out_of_range("myStack<> :: pop() on empty stack\n");
        T temp;
        temp = elems.back(); // back() returns an element
        elems.pop_back(); // pop_back() doesnot return anything
        return temp;

template<typename T>
T myStack<T> :: top() const
        return elems.back();


Recommended Answers

All 2 Replies

>vector<T> :: iterator ptr;
Change this to:

typename vector<T> :: iterator ptr;

Dependent types need explicit qualification with typename because C++ assumes they're not types and they can't be used in a declaration.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.