0
template <class T>
class Stack // an abstract stack base class
{
public:
Stack() {}
virtual void pop() = 0; // all pure
virtual T top() const = 0;
virtual void push(const T& t) = 0;
virtual unsigned int size() const = 0;
};
template <class T>
class VStack : public Stack<T> // a vector-based implementation
{
private:
vector<T> vec;
public:
VStack() {}
void pop() { vec.pop_back(); }
T top() const { return vec[vec.size() - 1];}
void push (const T& t) { vec.push_back(t);}
unsigned int size() const { return vec.size(); }
};

How do call these method into the main method?

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by StuXYZ
1

What doesn't work with this:

int
main()
{
  VStack<double> A;
  A.push(10.0);
  std::cout<<"A =="<<A.size()<<std::endl;
}

You remember that you have to use a template type in declaring VStack and that you might need to explicitly initialize the template if VStack and Stack are in separate files e.g. Add

template class Stack<double>;
template class VStack<double>;

HOWEVER: you may have made a complete mess, because you seem to have using namespace std; in your code, your names stack, etc are very close to the std names and you could easily be having real problems there.

Edited by mike_2000_17: Fixed formatting

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.