954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How do i output the contents of the stack with the functions Output ?

//Program to demonstrate the Stack class
#include
#include
using namespace std;

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

//constructor
template
Stack::Stack()
{
pool.reserve(10);
}

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

}


int main()
{

Stack dStack;
Stack 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;
}

Reg74
Light Poster
30 posts since Jul 2007
Reputation Points: 10
Solved Threads: 1
 

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;
  }
}
Hamrick
Posting Whiz
325 posts since Jun 2007
Reputation Points: 180
Solved Threads: 34
 

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

Reg74
Light Poster
30 posts since Jul 2007
Reputation Points: 10
Solved Threads: 1
 

Right. You prototype Output to take a T and try to pass a Stack. 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
Posting Whiz
325 posts since Jun 2007
Reputation Points: 180
Solved Threads: 34
 

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.

Reg74
Light Poster
30 posts since Jul 2007
Reputation Points: 10
Solved Threads: 1
 

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;
}
Hamrick
Posting Whiz
325 posts since Jun 2007
Reputation Points: 180
Solved Threads: 34
 

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

Reg74
Light Poster
30 posts since Jul 2007
Reputation Points: 10
Solved Threads: 1
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You