Class Template used for stack

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2007
Posts: 20
Reputation: bleonard989 is an unknown quantity at this point 
Solved Threads: 1
bleonard989 bleonard989 is offline Offline
Newbie Poster

Class Template used for stack

 
0
  #1
Feb 26th, 2008
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

  1. #ifndef STACK_H
  2. #define STACK_H
  3.  
  4. const int MAX_SIZE = 10;
  5.  
  6. class FullStack
  7. {};
  8.  
  9. class EmptyStack
  10. {};
  11.  
  12. template <class ItemType>
  13. class StackType
  14. {
  15. public:
  16. StackType (int size = MAX_SIZE);
  17. ~StackType();
  18. StackType(const StackType &right);
  19. void Clear ();
  20. bool IsFull () const;
  21. bool IsEmpty () const;
  22. void Push (ItemType item);
  23. ItemType Pop ();
  24. ItemType Top ();
  25. void Print ();
  26. const StackType<ItemType>& operator= (const StackType<ItemType>&);
  27.  
  28. private:
  29. ItemType *items;
  30. int stackSize;
  31. int top;
  32. };
  33.  
  34. #endif

Stack.cpp

  1. #include "Stack.h"
  2. #include <iostream>
  3. using namespace std;
  4.  
  5.  
  6. template <class ItemType>
  7. StackType<ItemType>::StackType(int size)
  8. {
  9. top = -1;
  10. stackSize = size;
  11. items = new int[stackSize];
  12. }
  13.  
  14. template <class ItemType>
  15. StackType<ItemType>::~StackType ()
  16. {
  17. delete [] items;
  18. }
  19.  
  20. template <class ItemType>
  21. StackType& StackType<ItemType>::operator = (const StackType &right)
  22. {
  23. if (&right != this)
  24. {
  25. if (stackSize != right.stackSize)
  26. {
  27. delete [] items;
  28. stackSize = right.stackSize;
  29. items = new ItemType[stackSize];
  30. }
  31. top = right.top;
  32.  
  33. for (int x = 0; x < stackSize; x++)
  34. {
  35. items[x] = right.items[x];
  36. }
  37. }
  38. return *this;
  39. }
  40.  
  41.  
  42. template <class ItemType>
  43. StackType<ItemType>::StackType (&right)
  44. {
  45. stackSize = right.stackSize;
  46. top = right.top;
  47. items = new int [stackSize];
  48.  
  49. for (int x = 0; x < stackSize; x++)
  50. {
  51. items[x] = right.items[x];
  52. }
  53. }
  54.  
  55. template <class ItemType>
  56. void StackType<ItemType>::Clear()
  57. {
  58. top = -1;
  59. }
  60.  
  61. template <class ItemType>
  62. bool StackType<ItemType>::IsEmpty() const
  63. {
  64. return (top == -1);
  65. }
  66.  
  67. template <class ItemType>
  68. bool StackType<ItemType>::IsFull() const
  69. {
  70. return (top == stackSize-1);
  71. }
  72.  
  73. template <class ItemType>
  74. void StackType<ItemType>::Push (ItemType item)
  75. {
  76. if (IsFull())
  77. {
  78. throw FullStack();
  79. }
  80. else
  81. {
  82. top++;
  83. items[top] = item;
  84. }
  85. }
  86.  
  87. template <class ItemType>
  88. ItemType StackType<ItemType>::Pop()
  89. {
  90. if (IsEmpty())
  91. {
  92. throw EmptyStack();
  93. }
  94. else
  95. {
  96. top--;
  97. }
  98. }
  99.  
  100. template <class ItemType>
  101. ItemType StackType<ItemType>:: Top()
  102. {
  103. if (IsEmpty())
  104. {
  105. throw EmptyStack();
  106. }
  107. return items[top];
  108. }
  109.  
  110. template <class ItemType>
  111. void StackType<ItemType>::Print()
  112. {
  113. int count;
  114. count = stackSize;
  115.  
  116. if (stackSize >= -1)
  117. {
  118. cout << "This stack contains: ";
  119. while (count >= -1)
  120. {
  121. cout << items[count]<< " ";
  122. }
  123. cout << endl;
  124. }
  125. else
  126. {
  127. throw EmptyStack();
  128. }
  129. }

This is the Client Code - where I think most of the problems are occurring

  1. #include <iostream>
  2. #include "Stack.h"
  3. using namespace std;
  4.  
  5. int main()
  6. {
  7.  
  8. StackType<int> stack;
  9.  
  10. float item;
  11. int num;
  12. int size;
  13.  
  14. cout << "How many items would you like in this stack (1 - 10): ";
  15. cin >> size;
  16.  
  17. if (size > 0 && size <= 10)
  18. size = size;
  19. else
  20. {
  21. cout << endl << "Not a valid number, using default of 10";
  22. size = MAX_SIZE;
  23. }
  24.  
  25. while (num != 0)
  26. {
  27. cout << "Please select from the following options:" << endl;
  28. cout << '\t' << "1 - Push an item" << endl;
  29. cout << '\t' << "2 - Pop an item" << endl;
  30. cout << '\t' << "3 - Get the top item" << endl;
  31. cout << '\t' << "4 - Determine if stack is full" << endl;
  32. cout << '\t' << "5 - Determine if stack is empty" << endl;
  33. cout << '\t' << "6 - Clear all items from stack" << endl;
  34. cout << '\t' << "7 - Print the stack" << endl;
  35. cout << '\t' << "8 - Copy stack" << endl;
  36. cout << '\t' << "9 - Assign one stack to another" << endl;
  37. cout << '\t' << "0 - Quit program" << endl;
  38. cout << endl;
  39. cout << '\t' << "Selection: ";
  40. cin >> num;
  41. cout << endl;
  42.  
  43. if (num == 1)
  44. {
  45. cout << "Enter an integer to push into the stack: ";
  46. cin >> item;
  47. try
  48. {
  49. stack.Push(item);
  50. cout << endl << item << " successfully pushed" << endl;
  51. }
  52. catch (FullStack exceptionObject)
  53. {
  54. cerr << endl << "Stack is full - unable to push item onto stack" << endl;
  55. }
  56. }
  57.  
  58. else if (num == 2)
  59. {
  60. try
  61. {
  62. cout << endl << stack.Pop() << " successfully popped" << endl;
  63. }
  64. catch (EmptyStack exceptionObject)
  65. {
  66. cerr << endl << "Stack is empty - unable to pop an item" << endl;
  67. }
  68. }
  69.  
  70. else if (num == 3)
  71. {
  72. try
  73. {
  74. cout << endl << stack.Top() << " is currently on top of the stack" << endl;
  75. }
  76. catch (EmptyStack exceptionObject)
  77. {
  78. cout << endl;
  79. cerr << "Cannot retrieve integer from top of list - list is currently empty" << endl;
  80. }
  81. }
  82.  
  83. else if (num == 4)
  84. {
  85. if (stack.IsFull() == true)
  86. {
  87. cout << endl << "The stack is currently full" << endl;
  88. }
  89. else
  90. {
  91. cout << endl << "The stack is NOT currently full" << endl;
  92. }
  93. }
  94.  
  95. else if (num == 5)
  96. {
  97. if (stack.IsEmpty () == true)
  98. {
  99. cout << endl << "The stack is currently empty" << endl;
  100. }
  101. else
  102. {
  103. cout << endl << "The stack is NOT currently empty" << endl;
  104. }
  105. }
  106.  
  107. else if (num == 6)
  108. {
  109. stack.Clear();
  110. cout << endl << "The stack was successfully cleared" << endl;
  111. }
  112.  
  113. else if (num == 7)
  114. {
  115. try
  116. {
  117. cout << endl;
  118. stack.Print ();
  119. cout << endl;
  120. }
  121. catch (EmptyStack exceptionObject)
  122. {
  123. cerr << endl << "There are no items in this stack" << endl;
  124. }
  125. }
  126.  
  127. else if (num == 8)
  128. {
  129. cout << endl << "Old Stack: ";
  130. stack.Print();
  131. cout << endl << "New Stack: ";
  132. stack.Print();
  133. cout << endl;
  134. }
  135.  
  136. else if (num == 9)
  137. {
  138. cout << endl << "Old Stack: ";
  139. stack.Print();
  140. cout << endl << "New Stack: ";
  141. stack.Print();
  142. cout << endl;
  143. }
  144.  
  145. else if (num < 0 || num > 9)
  146. {
  147. cout << endl << "Invalid input, please try again" << endl;
  148. }
  149. }
  150.  
  151. return 0;
  152. }

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
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 98
Reputation: codeaa is an unknown quantity at this point 
Solved Threads: 12
codeaa codeaa is offline Offline
Junior Poster in Training

Re: Class Template used for stack

 
0
  #2
Feb 26th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 20
Reputation: bleonard989 is an unknown quantity at this point 
Solved Threads: 1
bleonard989 bleonard989 is offline Offline
Newbie Poster

Re: Class Template used for stack

 
0
  #3
Feb 26th, 2008
Originally Posted by codeaa View Post
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.
  1. 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?
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 98
Reputation: codeaa is an unknown quantity at this point 
Solved Threads: 12
codeaa codeaa is offline Offline
Junior Poster in Training

Re: Class Template used for stack

 
0
  #4
Feb 27th, 2008
  1. StackType<ItemType>::StackType (&right)

Should be:

  1. StackType<ItemType>::StackType<ItemType> (const StackType<ItemType>& right)
Last edited by codeaa; Feb 27th, 2008 at 12:07 am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 20
Reputation: bleonard989 is an unknown quantity at this point 
Solved Threads: 1
bleonard989 bleonard989 is offline Offline
Newbie Poster

Re: Class Template used for stack

 
0
  #5
Feb 27th, 2008
Originally Posted by codeaa View Post
  1. StackType<ItemType>::StackType (&right)

Should be:

  1. StackType<ItemType>::StackType<ItemType> (const StackType<ItemType>& right)
when I do that
Stack.cpp:43: error: invalid function declaration

I get this error

Updated code:
  1. #ifndef STACK_H
  2. #define STACK_H
  3.  
  4. const int MAX_SIZE = 10;
  5.  
  6. class FullStack
  7. {};
  8.  
  9. class EmptyStack
  10. {};
  11.  
  12. template <class ItemType>
  13. class StackType
  14. {
  15. public:
  16. StackType(int size = MAX_SIZE);
  17. ~StackType();
  18. StackType(const StackType &right);
  19. void Clear();
  20. bool IsFull() const;
  21. bool IsEmpty() const;
  22. void Push(ItemType item);
  23. ItemType Pop();
  24. ItemType Top();
  25. void Print();
  26. StackType& operator=(const StackType &right);
  27.  
  28. private:
  29. ItemType *items;
  30. int stackSize;
  31. int top;
  32. };
  33.  
  34. #endif

Section that we are looking at on Stack.cpp
  1. #include "Stack.h"
  2. #include <iostream>
  3. using namespace std;
  4.  
  5.  
  6. template <class ItemType>
  7. StackType<ItemType>::StackType(int size)
  8. {
  9. top = -1;
  10. stackSize = size;
  11. items = new int[stackSize];
  12. }
  13.  
  14. template <class ItemType>
  15. StackType<ItemType>::~StackType ()
  16. {
  17. delete [] items;
  18. }
  19.  
  20. template <class ItemType>
  21. StackType<ItemType>& StackType<ItemType>::operator = (const StackType<ItemType> &right)
  22. {
  23. if (&right != this)
  24. {
  25. if (stackSize != right.stackSize)
  26. {
  27. delete [] items;
  28. stackSize = right.stackSize;
  29. items = new ItemType[stackSize];
  30. }
  31. top = right.top;
  32.  
  33. for (int x = 0; x < stackSize; x++)
  34. {
  35. items[x] = right.items[x];
  36. }
  37. }
  38. return *this;
  39. }
  40.  
  41. template <class ItemType>
  42. StackType<ItemType>::StackType<ItemType> (&right)
  43. {
  44. stackSize = right.stackSize;
  45. top = right.top;
  46. items = new int [stackSize];
  47.  
  48. for (int x = 0; x < stackSize; x++)
  49. {
  50. items[x] = right.items[x];
  51. }
  52. }

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
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 98
Reputation: codeaa is an unknown quantity at this point 
Solved Threads: 12
codeaa codeaa is offline Offline
Junior Poster in Training

Re: Class Template used for stack

 
0
  #6
Feb 27th, 2008
Sorry, I edited the code. You must have copied it before I made the change. Look at my message again.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 98
Reputation: codeaa is an unknown quantity at this point 
Solved Threads: 12
codeaa codeaa is offline Offline
Junior Poster in Training

Re: Class Template used for stack

 
0
  #7
Feb 27th, 2008
  1. StackType(const StackType &right);

In your .h file should be:

  1. StackType(const StackType<ItemType> &right);
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 20
Reputation: bleonard989 is an unknown quantity at this point 
Solved Threads: 1
bleonard989 bleonard989 is offline Offline
Newbie Poster

Re: Class Template used for stack

 
0
  #8
Feb 27th, 2008
Originally Posted by codeaa View Post
  1. StackType(const StackType &right);

In your .h file should be:

  1. StackType(const StackType<ItemType> &right);
I now get these errors when I go to compile:
  1. /tmp/ccXxm2AE.o(.text+0x123): In function `main':
  2. : undefined reference to `StackType<int>::StackType(int)'
  3. /tmp/ccXxm2AE.o(.text+0x401): In function `main':
  4. : undefined reference to `StackType<int>::Push(int)'
  5. /tmp/ccXxm2AE.o(.text+0x4ed): In function `main':
  6. : undefined reference to `StackType<int>::Pop()'
  7. /tmp/ccXxm2AE.o(.text+0x5c4): In function `main':
  8. : undefined reference to `StackType<int>::Top()'
  9. /tmp/ccXxm2AE.o(.text+0x68c): In function `main':
  10. : undefined reference to `StackType<int>::IsFull() const'
  11. /tmp/ccXxm2AE.o(.text+0x725): In function `main':
  12. : undefined reference to `StackType<int>::IsEmpty() const'
  13. /tmp/ccXxm2AE.o(.text+0x7ba): In function `main':
  14. : undefined reference to `StackType<int>::Clear()'
  15. /tmp/ccXxm2AE.o(.text+0x824): In function `main':
  16. : undefined reference to `StackType<int>::Print()'
  17. /tmp/ccXxm2AE.o(.text+0x8f2): In function `main':
  18. : undefined reference to `StackType<int>::Print()'
  19. /tmp/ccXxm2AE.o(.text+0x927): In function `main':
  20. : undefined reference to `StackType<int>::Print()'
  21. /tmp/ccXxm2AE.o(.text+0x980): In function `main':
  22. : undefined reference to `StackType<int>::Print()'
  23. /tmp/ccXxm2AE.o(.text+0x9b5): In function `main':
  24. : undefined reference to `StackType<int>::Print()'
  25. /tmp/ccXxm2AE.o(.text+0xa2b): In function `main':
  26. : undefined reference to `StackType<int>::~StackType()'
  27. /tmp/ccXxm2AE.o(.text+0xa49): In function `main':
  28. : undefined reference to `StackType<int>::~StackType()'
  29. collect2: ld returned 1 exit status

My .h file is now changed to
  1. StackType(const StackType<ItemType> &right);


My .cpp file is now changed to
  1. StackType<ItemType>::StackType<ItemType> (const StackType<ItemType>& right)
Last edited by bleonard989; Feb 27th, 2008 at 12:37 am.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 98
Reputation: codeaa is an unknown quantity at this point 
Solved Threads: 12
codeaa codeaa is offline Offline
Junior Poster in Training

Re: Class Template used for stack

 
0
  #9
Feb 27th, 2008
Post all of the code.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 20
Reputation: bleonard989 is an unknown quantity at this point 
Solved Threads: 1
bleonard989 bleonard989 is offline Offline
Newbie Poster

Re: Class Template used for stack

 
0
  #10
Feb 27th, 2008
Stack.h

  1. #ifndef STACK_H
  2. #define STACK_H
  3.  
  4.  
  5. const int MAX_SIZE = 10;
  6.  
  7. class FullStack
  8. {};
  9.  
  10. class EmptyStack
  11. {};
  12.  
  13. template <class ItemType>
  14. class StackType
  15. {
  16. public:
  17. StackType(int size = MAX_SIZE);
  18. ~StackType();
  19. StackType(const StackType<ItemType> &right);
  20. void Clear();
  21. bool IsFull() const;
  22. bool IsEmpty() const;
  23. void Push(ItemType item);
  24. ItemType Pop();
  25. ItemType Top();
  26. void Print();
  27. StackType& operator=(const StackType<ItemType> &right);
  28.  
  29. private:
  30. ItemType *items;
  31. int stackSize;
  32. int top;
  33. };
  34.  
  35. #endif

Stack.cpp

  1. #include "Stack.h"
  2. #include <iostream>
  3. using namespace std;
  4.  
  5.  
  6. template <class ItemType>
  7. StackType<ItemType>::StackType(int size)
  8. {
  9. top = -1;
  10. stackSize = size;
  11. items = new int[stackSize];
  12. }
  13.  
  14. template <class ItemType>
  15. StackType<ItemType>::~StackType ()
  16. {
  17. delete [] items;
  18. }
  19.  
  20. template <class ItemType>
  21. StackType<ItemType>& StackType<ItemType>::operator = (const StackType<ItemType> &right)
  22. {
  23. if (&right != this)
  24. {
  25. if (stackSize != right.stackSize)
  26. {
  27. delete [] items;
  28. stackSize = right.stackSize;
  29. items = new ItemType[stackSize];
  30. }
  31. top = right.top;
  32.  
  33. for (int x = 0; x < stackSize; x++)
  34. {
  35. items[x] = right.items[x];
  36. }
  37. }
  38. return *this;
  39. }
  40.  
  41. template <class ItemType>
  42. StackType<ItemType>::StackType<ItemType> (const StackType<ItemType>& right)
  43. {
  44. stackSize = right.stackSize;
  45. top = right.top;
  46. items = new int [stackSize];
  47.  
  48. for (int x = 0; x < stackSize; x++)
  49. {
  50. items[x] = right.items[x];
  51. }
  52. }
  53.  
  54. template <class ItemType>
  55. void StackType<ItemType>::Clear()
  56. {
  57. top = -1;
  58. }
  59.  
  60. template <class ItemType>
  61. bool StackType<ItemType>::IsEmpty() const
  62. {
  63. return (top == -1);
  64. }
  65.  
  66. template <class ItemType>
  67. bool StackType<ItemType>::IsFull() const
  68. {
  69. return (top == stackSize-1);
  70. }
  71.  
  72. template <class ItemType>
  73. void StackType<ItemType>::Push (ItemType item)
  74. {
  75. if (IsFull())
  76. {
  77. throw FullStack();
  78. }
  79. else
  80. {
  81. top++;
  82. items[top] = item;
  83. }
  84. }
  85.  
  86. template <class ItemType>
  87. ItemType StackType<ItemType>::Pop()
  88. {
  89. if (IsEmpty())
  90. {
  91. throw EmptyStack();
  92. }
  93. else
  94. {
  95. top--;
  96. }
  97. }
  98.  
  99. template <class ItemType>
  100. ItemType StackType<ItemType>:: Top()
  101. {
  102. if (IsEmpty())
  103. {
  104. throw EmptyStack();
  105. }
  106. return items[top];
  107. }
  108. template <class ItemType>
  109. void StackType<ItemType>::Print()
  110. {
  111. int count;
  112. count = stackSize;
  113.  
  114. if (stackSize >= -1)
  115. {
  116. cout << "This stack contains: ";
  117. while (count >= -1)
  118. {
  119. cout << items[count]<< " ";
  120. }
  121. cout << endl;
  122. }
  123. else
  124. {
  125. throw EmptyStack();
  126. }
  127. }

If you see anything that will not work in the code, will you give me the heads up too? Thanks,
Ben
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC