| | |
How do i output the contents of the stack with the functions Output ?
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Jul 2007
Posts: 30
Reputation:
Solved Threads: 1
//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;
}
#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;
}
From the prototype I'd guess this.
I don't understand why you have a parameter. It seems you want to output the whole stack.
C++ Syntax (Toggle Plain Text)
template<class T> void Stack<T>::Output(T element) { cout<< element; }
C++ Syntax (Toggle Plain Text)
template<class T> void Stack<T>::Output() { for ( int i = 0; i < pool.size(); ++i ) { cout<< pool[i] <<endl; } }
The truth does not change according to our ability to stomach it.
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)
// Output takes a T parameter dStack.Output( dStack.top() ); // Output takes no parameters dStack.Output();
Last edited by Hamrick; Jul 19th, 2007 at 10:09 am.
The truth does not change according to our ability to stomach it.
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)
//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; }
The truth does not change according to our ability to stomach it.
![]() |
Similar Threads
- Question on nested structure (C)
- Dr Scheme problem about parse a list of symbols (IT Professionals' Lounge)
- problem with storage ..need help (C++)
- Implementation of Stacks and Queues (C)
- Reverse Output (Stack) (C++)
Other Threads in the C++ Forum
- Previous Thread: Preprocessor definitions on the command line
- Next Thread: Maximum possible nodes in any link list
| Thread Tools | Search this Thread |
api array arrays based beginner binary bmp c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib library linkedlist linker linux list loop looping loops map math matrix memory newbie news number output pointer problem program programming project python random read recursion recursive reference return rpg simple string strings studio temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets





