Program to demonstrate the Stack class

#include <iostream>
#include <vector>
using namespace std;

 template<class T>
    class Stack
    {
       public:
            Stack();
            bool empty() const;
            T top() const;
            T pop();
            void push(T element);
            void Output(T element);
       private:
            vector <T> pool;
     };  

     //constructor
     template<class T>
     Stack<T>::Stack()
     {
        pool.reserve(10);
     }    

     //function to output the values in the stack
      template<class T>
      void Stack<T>::Output(T element)
      {

      }     



int main()
{

   Stack<double> dStack;
   Stack<string> sStack;

   cout << "Pushing 3.4, 5.5, 2.2, 6.7 -> on to dStack" << endl;
   dStack.push(3.4);
   dStack.push(5.5);
   dStack.push(2.2);
   dStack.push(6.7);
   cout << endl;

   cout << "Pushing Sawubona, Molo, Lotjha, Dumela, Hi, Hallo" << endl;
   cout << "Ndi Masiari, Avuxeni -> on to sStack" << endl;
   sStack.push("Sawubona");
   sStack.push("Molo");
   sStack.push("Lotjha");
   sStack.push("Dumela");
   sStack.push("Hi");
   sStack.push("Hallo");
   sStack.push("Ndi Masiari");
   sStack.push("Avuxeni");

   cout << endl << "dStack:" << endl;
   Output(dStack);
   cout << endl << "sStack:" << endl;
   Output(sStack);      

   return 0;
}

Recommended Answers

All 6 Replies

From the prototype I'd guess this.

template<class T>
void Stack<T>::Output(T element)
{
  cout<< element;
}

I don't understand why you have a parameter. It seems you want to output the whole stack.

template<class T>
void Stack<T>::Output()
{
  for ( int i = 0; i < pool.size(); ++i ) {
    cout<< pool[i] <<endl;
  }
}

My compiler is complaining about the call to the function Output as in Output (dStack); saying that Output is undeclared ?

Right. You prototype Output to take a T and try to pass a Stack<T>. I guess you'd call it like this for the two different functions I wrote.

// Output takes a T parameter
dStack.Output( dStack.top() );

// Output takes no parameters
dStack.Output();

Hamrick i'm still getting compiler errors.
Could you copy the code into a compiler to see what it does or is that too much to ask?
I would just love to see what the problem was ?
Thanks for all your inputs thus far.

I compiled it and everything's like I said. Change how you create Output then change how you call it. I also added a body for push so that it runs, I hope you don't mind.

//Program to demonstrate the Stack class
#include <iostream>
#include <vector>
using namespace std;

template<class T>
class Stack
{
public:
  Stack();
  bool empty() const;
  T top() const;
  T pop();
  void push(T element) { pool.push_back( element ); }
  void Output();
private:
  vector <T> pool;
};

//constructor
template<class T>
Stack<T>::Stack()
{
  pool.reserve(10);
}

//function to output the values in the stack
template<class T>
void Stack<T>::Output()
{
  for ( int i = 0; i < pool.size(); ++i ) {
    cout<< pool[i] <<endl;
  }
}

int main()
{

  Stack<double> dStack;
  Stack<string> sStack;

  cout << "Pushing 3.4, 5.5, 2.2, 6.7 -> on to dStack" << endl;
  dStack.push(3.4);
  dStack.push(5.5);
  dStack.push(2.2);
  dStack.push(6.7);
  cout << endl;

  cout << "Pushing Sawubona, Molo, Lotjha, Dumela, Hi, Hallo" << endl;
  cout << "Ndi Masiari, Avuxeni -> on to sStack" << endl;
  sStack.push("Sawubona");
  sStack.push("Molo");
  sStack.push("Lotjha");
  sStack.push("Dumela");
  sStack.push("Hi");
  sStack.push("Hallo");
  sStack.push("Ndi Masiari");
  sStack.push("Avuxeni");

  cout << endl << "dStack:" << endl;
  dStack.Output();
  cout << endl << "sStack:" << endl;
  dStack.Output();

  return 0;
}

Thanks alot HAMRICK !!!!!!!!!!!!!!!!!!!!!!!!!

Be a part of the DaniWeb community

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