| | |
Class Template used for stack
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2007
Posts: 20
Reputation:
Solved Threads: 1
These are the errors I am getting. I have never worked with classes before, I posted before but have done some work to it since then, and have gotten less errors. The main problems are the push function, the copy constructor, and the function that creates a new stack.
prog4.cpp: In function `int main()':
prog4.cpp:49: warning: passing `float' for converting 1 of `void StackType<ItemType>:: Push(ItemType) [with ItemType = int]'
Stack.cpp:21: error: expected constructor, destructor, or type conversion before '&' token
Stack.cpp:42: error: expected constructor, destructor, or type conversion before '(' token
Stack.cpp
This is the Client Code - where I think most of the problems are occurring
Like I said, I have never worked with class templates or stacks before, so I dont really know what I'm doing. Any help, suggestions, criticism would be gladly accepted... Thanks
prog4.cpp: In function `int main()':
prog4.cpp:49: warning: passing `float' for converting 1 of `void StackType<ItemType>:: Push(ItemType) [with ItemType = int]'
Stack.cpp:21: error: expected constructor, destructor, or type conversion before '&' token
Stack.cpp:42: error: expected constructor, destructor, or type conversion before '(' token
C++ Syntax (Toggle Plain Text)
#ifndef STACK_H #define STACK_H const int MAX_SIZE = 10; class FullStack {}; class EmptyStack {}; template <class ItemType> class StackType { public: StackType (int size = MAX_SIZE); ~StackType(); StackType(const StackType &right); void Clear (); bool IsFull () const; bool IsEmpty () const; void Push (ItemType item); ItemType Pop (); ItemType Top (); void Print (); const StackType<ItemType>& operator= (const StackType<ItemType>&); private: ItemType *items; int stackSize; int top; }; #endif
Stack.cpp
C++ Syntax (Toggle Plain Text)
#include "Stack.h" #include <iostream> using namespace std; template <class ItemType> StackType<ItemType>::StackType(int size) { top = -1; stackSize = size; items = new int[stackSize]; } template <class ItemType> StackType<ItemType>::~StackType () { delete [] items; } template <class ItemType> StackType& StackType<ItemType>::operator = (const StackType &right) { if (&right != this) { if (stackSize != right.stackSize) { delete [] items; stackSize = right.stackSize; items = new ItemType[stackSize]; } top = right.top; for (int x = 0; x < stackSize; x++) { items[x] = right.items[x]; } } return *this; } template <class ItemType> StackType<ItemType>::StackType (&right) { stackSize = right.stackSize; top = right.top; items = new int [stackSize]; for (int x = 0; x < stackSize; x++) { items[x] = right.items[x]; } } template <class ItemType> void StackType<ItemType>::Clear() { top = -1; } template <class ItemType> bool StackType<ItemType>::IsEmpty() const { return (top == -1); } template <class ItemType> bool StackType<ItemType>::IsFull() const { return (top == stackSize-1); } template <class ItemType> void StackType<ItemType>::Push (ItemType item) { if (IsFull()) { throw FullStack(); } else { top++; items[top] = item; } } template <class ItemType> ItemType StackType<ItemType>::Pop() { if (IsEmpty()) { throw EmptyStack(); } else { top--; } } template <class ItemType> ItemType StackType<ItemType>:: Top() { if (IsEmpty()) { throw EmptyStack(); } return items[top]; } template <class ItemType> void StackType<ItemType>::Print() { int count; count = stackSize; if (stackSize >= -1) { cout << "This stack contains: "; while (count >= -1) { cout << items[count]<< " "; } cout << endl; } else { throw EmptyStack(); } }
This is the Client Code - where I think most of the problems are occurring
C++ Syntax (Toggle Plain Text)
#include <iostream> #include "Stack.h" using namespace std; int main() { StackType<int> stack; float item; int num; int size; cout << "How many items would you like in this stack (1 - 10): "; cin >> size; if (size > 0 && size <= 10) size = size; else { cout << endl << "Not a valid number, using default of 10"; size = MAX_SIZE; } while (num != 0) { cout << "Please select from the following options:" << endl; cout << '\t' << "1 - Push an item" << endl; cout << '\t' << "2 - Pop an item" << endl; cout << '\t' << "3 - Get the top item" << endl; cout << '\t' << "4 - Determine if stack is full" << endl; cout << '\t' << "5 - Determine if stack is empty" << endl; cout << '\t' << "6 - Clear all items from stack" << endl; cout << '\t' << "7 - Print the stack" << endl; cout << '\t' << "8 - Copy stack" << endl; cout << '\t' << "9 - Assign one stack to another" << endl; cout << '\t' << "0 - Quit program" << endl; cout << endl; cout << '\t' << "Selection: "; cin >> num; cout << endl; if (num == 1) { cout << "Enter an integer to push into the stack: "; cin >> item; try { stack.Push(item); cout << endl << item << " successfully pushed" << endl; } catch (FullStack exceptionObject) { cerr << endl << "Stack is full - unable to push item onto stack" << endl; } } else if (num == 2) { try { cout << endl << stack.Pop() << " successfully popped" << endl; } catch (EmptyStack exceptionObject) { cerr << endl << "Stack is empty - unable to pop an item" << endl; } } else if (num == 3) { try { cout << endl << stack.Top() << " is currently on top of the stack" << endl; } catch (EmptyStack exceptionObject) { cout << endl; cerr << "Cannot retrieve integer from top of list - list is currently empty" << endl; } } else if (num == 4) { if (stack.IsFull() == true) { cout << endl << "The stack is currently full" << endl; } else { cout << endl << "The stack is NOT currently full" << endl; } } else if (num == 5) { if (stack.IsEmpty () == true) { cout << endl << "The stack is currently empty" << endl; } else { cout << endl << "The stack is NOT currently empty" << endl; } } else if (num == 6) { stack.Clear(); cout << endl << "The stack was successfully cleared" << endl; } else if (num == 7) { try { cout << endl; stack.Print (); cout << endl; } catch (EmptyStack exceptionObject) { cerr << endl << "There are no items in this stack" << endl; } } else if (num == 8) { cout << endl << "Old Stack: "; stack.Print(); cout << endl << "New Stack: "; stack.Print(); cout << endl; } else if (num == 9) { cout << endl << "Old Stack: "; stack.Print(); cout << endl << "New Stack: "; stack.Print(); cout << endl; } else if (num < 0 || num > 9) { cout << endl << "Invalid input, please try again" << endl; } } return 0; }
Like I said, I have never worked with class templates or stacks before, so I dont really know what I'm doing. Any help, suggestions, criticism would be gladly accepted... Thanks
•
•
Join Date: Nov 2007
Posts: 20
Reputation:
Solved Threads: 1
•
•
•
•
StackType& StackType<ItemType>::operator = (const StackType &right)
Needs to be:
StackType<ItemType>& StackType<ItemType>::operator = (const StackType<ItemType> &right)
The other error is the same issue.
C++ Syntax (Toggle Plain Text)
StackType<ItemType>& StackType<ItemType>::StackType(const StackType<ItemType> &right)
when I put this in for the other function, it comes up with 2 errors
Stack.cpp:43: error: return type specification for constructor invalid
Stack.cpp:43: error: cannot declare reference to `void'
So I am more than likely not doing that correctly, how should this be implemented?
•
•
Join Date: Feb 2008
Posts: 98
Reputation:
Solved Threads: 12
cpp Syntax (Toggle Plain Text)
StackType<ItemType>::StackType (&right)
Should be:
cpp Syntax (Toggle Plain Text)
StackType<ItemType>::StackType<ItemType> (const StackType<ItemType>& right)
Last edited by codeaa; Feb 27th, 2008 at 12:07 am.
•
•
Join Date: Nov 2007
Posts: 20
Reputation:
Solved Threads: 1
•
•
•
•
cpp Syntax (Toggle Plain Text)
StackType<ItemType>::StackType (&right)
Should be:
cpp Syntax (Toggle Plain Text)
StackType<ItemType>::StackType<ItemType> (const StackType<ItemType>& right)
Stack.cpp:43: error: invalid function declaration
I get this error
Updated code:
C++ Syntax (Toggle Plain Text)
#ifndef STACK_H #define STACK_H const int MAX_SIZE = 10; class FullStack {}; class EmptyStack {}; template <class ItemType> class StackType { public: StackType(int size = MAX_SIZE); ~StackType(); StackType(const StackType &right); void Clear(); bool IsFull() const; bool IsEmpty() const; void Push(ItemType item); ItemType Pop(); ItemType Top(); void Print(); StackType& operator=(const StackType &right); private: ItemType *items; int stackSize; int top; }; #endif
Section that we are looking at on Stack.cpp
C++ Syntax (Toggle Plain Text)
#include "Stack.h" #include <iostream> using namespace std; template <class ItemType> StackType<ItemType>::StackType(int size) { top = -1; stackSize = size; items = new int[stackSize]; } template <class ItemType> StackType<ItemType>::~StackType () { delete [] items; } template <class ItemType> StackType<ItemType>& StackType<ItemType>::operator = (const StackType<ItemType> &right) { if (&right != this) { if (stackSize != right.stackSize) { delete [] items; stackSize = right.stackSize; items = new ItemType[stackSize]; } top = right.top; for (int x = 0; x < stackSize; x++) { items[x] = right.items[x]; } } return *this; } template <class ItemType> StackType<ItemType>::StackType<ItemType> (&right) { stackSize = right.stackSize; top = right.top; items = new int [stackSize]; for (int x = 0; x < stackSize; x++) { items[x] = right.items[x]; } }
SORRY for bugging you so much, I have never used class templates before and the book and notes make me even more confused than I was going into this project
Thanks for your help
•
•
Join Date: Feb 2008
Posts: 98
Reputation:
Solved Threads: 12
cpp Syntax (Toggle Plain Text)
StackType(const StackType &right);
In your .h file should be:
cpp Syntax (Toggle Plain Text)
StackType(const StackType<ItemType> &right);
•
•
Join Date: Nov 2007
Posts: 20
Reputation:
Solved Threads: 1
•
•
•
•
cpp Syntax (Toggle Plain Text)
StackType(const StackType &right);
In your .h file should be:
cpp Syntax (Toggle Plain Text)
StackType(const StackType<ItemType> &right);
C++ Syntax (Toggle Plain Text)
/tmp/ccXxm2AE.o(.text+0x123): In function `main': : undefined reference to `StackType<int>::StackType(int)' /tmp/ccXxm2AE.o(.text+0x401): In function `main': : undefined reference to `StackType<int>::Push(int)' /tmp/ccXxm2AE.o(.text+0x4ed): In function `main': : undefined reference to `StackType<int>::Pop()' /tmp/ccXxm2AE.o(.text+0x5c4): In function `main': : undefined reference to `StackType<int>::Top()' /tmp/ccXxm2AE.o(.text+0x68c): In function `main': : undefined reference to `StackType<int>::IsFull() const' /tmp/ccXxm2AE.o(.text+0x725): In function `main': : undefined reference to `StackType<int>::IsEmpty() const' /tmp/ccXxm2AE.o(.text+0x7ba): In function `main': : undefined reference to `StackType<int>::Clear()' /tmp/ccXxm2AE.o(.text+0x824): In function `main': : undefined reference to `StackType<int>::Print()' /tmp/ccXxm2AE.o(.text+0x8f2): In function `main': : undefined reference to `StackType<int>::Print()' /tmp/ccXxm2AE.o(.text+0x927): In function `main': : undefined reference to `StackType<int>::Print()' /tmp/ccXxm2AE.o(.text+0x980): In function `main': : undefined reference to `StackType<int>::Print()' /tmp/ccXxm2AE.o(.text+0x9b5): In function `main': : undefined reference to `StackType<int>::Print()' /tmp/ccXxm2AE.o(.text+0xa2b): In function `main': : undefined reference to `StackType<int>::~StackType()' /tmp/ccXxm2AE.o(.text+0xa49): In function `main': : undefined reference to `StackType<int>::~StackType()' collect2: ld returned 1 exit status
My .h file is now changed to
C++ Syntax (Toggle Plain Text)
StackType(const StackType<ItemType> &right);
My .cpp file is now changed to
C++ Syntax (Toggle Plain Text)
StackType<ItemType>::StackType<ItemType> (const StackType<ItemType>& right)
Last edited by bleonard989; Feb 27th, 2008 at 12:37 am.
•
•
Join Date: Nov 2007
Posts: 20
Reputation:
Solved Threads: 1
Stack.h
Stack.cpp
If you see anything that will not work in the code, will you give me the heads up too? Thanks,
Ben
C++ Syntax (Toggle Plain Text)
#ifndef STACK_H #define STACK_H const int MAX_SIZE = 10; class FullStack {}; class EmptyStack {}; template <class ItemType> class StackType { public: StackType(int size = MAX_SIZE); ~StackType(); StackType(const StackType<ItemType> &right); void Clear(); bool IsFull() const; bool IsEmpty() const; void Push(ItemType item); ItemType Pop(); ItemType Top(); void Print(); StackType& operator=(const StackType<ItemType> &right); private: ItemType *items; int stackSize; int top; }; #endif
Stack.cpp
C++ Syntax (Toggle Plain Text)
#include "Stack.h" #include <iostream> using namespace std; template <class ItemType> StackType<ItemType>::StackType(int size) { top = -1; stackSize = size; items = new int[stackSize]; } template <class ItemType> StackType<ItemType>::~StackType () { delete [] items; } template <class ItemType> StackType<ItemType>& StackType<ItemType>::operator = (const StackType<ItemType> &right) { if (&right != this) { if (stackSize != right.stackSize) { delete [] items; stackSize = right.stackSize; items = new ItemType[stackSize]; } top = right.top; for (int x = 0; x < stackSize; x++) { items[x] = right.items[x]; } } return *this; } template <class ItemType> StackType<ItemType>::StackType<ItemType> (const StackType<ItemType>& right) { stackSize = right.stackSize; top = right.top; items = new int [stackSize]; for (int x = 0; x < stackSize; x++) { items[x] = right.items[x]; } } template <class ItemType> void StackType<ItemType>::Clear() { top = -1; } template <class ItemType> bool StackType<ItemType>::IsEmpty() const { return (top == -1); } template <class ItemType> bool StackType<ItemType>::IsFull() const { return (top == stackSize-1); } template <class ItemType> void StackType<ItemType>::Push (ItemType item) { if (IsFull()) { throw FullStack(); } else { top++; items[top] = item; } } template <class ItemType> ItemType StackType<ItemType>::Pop() { if (IsEmpty()) { throw EmptyStack(); } else { top--; } } template <class ItemType> ItemType StackType<ItemType>:: Top() { if (IsEmpty()) { throw EmptyStack(); } return items[top]; } template <class ItemType> void StackType<ItemType>::Print() { int count; count = stackSize; if (stackSize >= -1) { cout << "This stack contains: "; while (count >= -1) { cout << items[count]<< " "; } cout << endl; } else { throw EmptyStack(); } }
If you see anything that will not work in the code, will you give me the heads up too? Thanks,
Ben
![]() |
Similar Threads
- Help on stack , queue, palindrome program... (C++)
- How do i output the contents of the stack with the functions Output ? (C++)
- Help with Stacks Using Class Templates (C++)
- Template class (C)
- can ANYONE help? need to make a queue (C)
- accessing private data members (C++)
Other Threads in the C++ Forum
- Previous Thread: Print highest
- Next Thread: Windows Delayed Write failed
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets





