Hi,
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?
#include<iostream>
#include<vector>
#include<string>
#include<stdexcept>
using namespace std;
template<typename T>
class myStack {
protected:
vector <T> elems;
public:
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;
ptr++;
cout << *ptr << endl;
//}
}
};
template<typename T>
void myStack<T> :: clear()
{
elems.clear();
}
template<typename T>
void myStack<T> :: push(T const &ob)
{
cout << "TRACE: push()" << endl;
elems.push_back(ob);
}
/*
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();
}