944,144 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 884
  • C++ RSS
Jul 19th, 2007
0

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

Expand Post »
//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>:tack()
{
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;
}
Similar Threads
Reputation Points: 10
Solved Threads: 1
Light Poster
Reg74 is offline Offline
30 posts
since Jul 2007
Jul 19th, 2007
0

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

From the prototype I'd guess this.
C++ Syntax (Toggle Plain Text)
  1. template<class T>
  2. void Stack<T>::Output(T element)
  3. {
  4. cout<< element;
  5. }
I don't understand why you have a parameter. It seems you want to output the whole stack.
C++ Syntax (Toggle Plain Text)
  1. template<class T>
  2. void Stack<T>::Output()
  3. {
  4. for ( int i = 0; i < pool.size(); ++i ) {
  5. cout<< pool[i] <<endl;
  6. }
  7. }
Reputation Points: 180
Solved Threads: 34
Posting Whiz
Hamrick is offline Offline
322 posts
since Jun 2007
Jul 19th, 2007
0

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

My compiler is complaining about the call to the function Output as in Output (dStack); saying that Output is undeclared ?
Reputation Points: 10
Solved Threads: 1
Light Poster
Reg74 is offline Offline
30 posts
since Jul 2007
Jul 19th, 2007
0

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

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.
C++ Syntax (Toggle Plain Text)
  1. // Output takes a T parameter
  2. dStack.Output( dStack.top() );
  3.  
  4. // Output takes no parameters
  5. dStack.Output();
Last edited by Hamrick; Jul 19th, 2007 at 10:09 am.
Reputation Points: 180
Solved Threads: 34
Posting Whiz
Hamrick is offline Offline
322 posts
since Jun 2007
Jul 19th, 2007
0

Re: How do i output the contents of the stack with the functions 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.
Reputation Points: 10
Solved Threads: 1
Light Poster
Reg74 is offline Offline
30 posts
since Jul 2007
Jul 19th, 2007
0

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

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.
C++ Syntax (Toggle Plain Text)
  1. //Program to demonstrate the Stack class
  2. #include <iostream>
  3. #include <vector>
  4. using namespace std;
  5.  
  6. template<class T>
  7. class Stack
  8. {
  9. public:
  10. Stack();
  11. bool empty() const;
  12. T top() const;
  13. T pop();
  14. void push(T element) { pool.push_back( element ); }
  15. void Output();
  16. private:
  17. vector <T> pool;
  18. };
  19.  
  20. //constructor
  21. template<class T>
  22. Stack<T>::Stack()
  23. {
  24. pool.reserve(10);
  25. }
  26.  
  27. //function to output the values in the stack
  28. template<class T>
  29. void Stack<T>::Output()
  30. {
  31. for ( int i = 0; i < pool.size(); ++i ) {
  32. cout<< pool[i] <<endl;
  33. }
  34. }
  35.  
  36. int main()
  37. {
  38.  
  39. Stack<double> dStack;
  40. Stack<string> sStack;
  41.  
  42. cout << "Pushing 3.4, 5.5, 2.2, 6.7 -> on to dStack" << endl;
  43. dStack.push(3.4);
  44. dStack.push(5.5);
  45. dStack.push(2.2);
  46. dStack.push(6.7);
  47. cout << endl;
  48.  
  49. cout << "Pushing Sawubona, Molo, Lotjha, Dumela, Hi, Hallo" << endl;
  50. cout << "Ndi Masiari, Avuxeni -> on to sStack" << endl;
  51. sStack.push("Sawubona");
  52. sStack.push("Molo");
  53. sStack.push("Lotjha");
  54. sStack.push("Dumela");
  55. sStack.push("Hi");
  56. sStack.push("Hallo");
  57. sStack.push("Ndi Masiari");
  58. sStack.push("Avuxeni");
  59.  
  60. cout << endl << "dStack:" << endl;
  61. dStack.Output();
  62. cout << endl << "sStack:" << endl;
  63. dStack.Output();
  64.  
  65. return 0;
  66. }
Reputation Points: 180
Solved Threads: 34
Posting Whiz
Hamrick is offline Offline
322 posts
since Jun 2007
Jul 20th, 2007
0

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

Thanks alot HAMRICK !!!!!!!!!!!!!!!!!!!!!!!!!
Reputation Points: 10
Solved Threads: 1
Light Poster
Reg74 is offline Offline
30 posts
since Jul 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Preprocessor definitions on the command line
Next Thread in C++ Forum Timeline: Maximum possible nodes in any link list





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC