C++ Compare stacks help!

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

Join Date: Mar 2008
Posts: 365
Reputation: NinjaLink is an unknown quantity at this point 
Solved Threads: 0
NinjaLink NinjaLink is offline Offline
Posting Whiz

C++ Compare stacks help!

 
0
  #1
33 Days Ago
Can someone help me determine if 2 stacks are the same? I am fairly new to stacks and need help. My header file is also below. Currently I am getting an error message using "==" and "!=" in my if statement. If someone can help, I will appreciate it! Thanks


error messages:

12 no match for 'operator==' in 's1 == s2'
21 no match for 'operator!=' in 's1 != s2'
26 no match for 'operator==' in 's1 == s2'
37 no match for 'operator!=' in 's1 != s2'


Expected Output or similar

  1. empty stacks are equal
  2. s1: 20 10
  3. s2: 10
  4. s1 is not equal to s2
  5. s2: 20 10
  6. s1 is equal to s2
  7. s1: 20 30
  8. s1 is not equal to s2


main.cpp


  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include "stack.h"
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. stackType<int> s1;
  10. stackType<int> s2;
  11.  
  12. if (s1 == s2)
  13. {
  14. cout << "empty stacks are equal" << endl;
  15. }
  16. s1.push(10); s1.push(20);
  17. s2.push(10);
  18. cout << "s1: ";
  19. cout << "s2: ";
  20. cout << "s1 ";
  21. if (s1 != s2)
  22. cout << "!";
  23. cout << "= s2" << endl;
  24. s2.push(20);
  25. cout << "s2: ";
  26. cout << "s1 ";
  27. if (s1 == s2)
  28. {
  29. cout << "is equal ";
  30. } else {
  31. cout << "is not equal " << endl;
  32. }
  33. cout << "s2" << endl;
  34. s1.pop(); s1.pop();
  35. s1.push(30); s1.push(20);
  36. cout << "s1: ";
  37. cout << "s1 ";
  38. if (s1 != s2)
  39. cout << " is";
  40. cout << " not equal to s2" << endl;
  41.  
  42. system ("pause");
  43. return 0;
  44. }




header file


  1. #ifndef H_StackType
  2. #define H_StackType
  3.  
  4. #include <iostream>
  5. #include <cassert>
  6.  
  7. using namespace std;
  8.  
  9. template<class Type>
  10.  
  11. class stackType
  12. {
  13.  
  14. public:
  15. const stackType<Type>& operator=(const stackType<Type>&);
  16. void initializeStack();
  17. bool isEmptyStack();
  18. bool isFullStack();
  19. void destroyStack();
  20. void push(const Type& newItem);
  21. Type top();
  22. void pop();
  23. stackType(int stackSize = 100);
  24. stackType(const stackType<Type>& otherStack);
  25. ~stackType();
  26.  
  27.  
  28. private:
  29. int maxStackSize;
  30. int stackTop;
  31. Type *list;
  32. void copyStack(const stackType<Type>& otherStack);
  33. };
  34.  
  35. template<class Type>
  36. void stackType<Type>::copyStack(const stackType<Type>& otherStack)
  37. {
  38. delete [] list;
  39. maxStackSize = otherStack.maxStackSize;
  40. stackTop = otherStack.stackTop;
  41.  
  42. list = new Type[maxStackSize];
  43. assert(list != NULL);
  44.  
  45. for(int j = 0; j < stackTop; j++)
  46. list[j] = otherStack.list[j];
  47.  
  48. }
  49.  
  50. template<class Type>
  51. stackType<Type>::stackType(const stackType<Type>& otherStack)
  52. {
  53. list = NULL;
  54.  
  55. copyStack(otherStack);
  56.  
  57. }
  58.  
  59. template<class Type>
  60. const stackType<Type>& stackType<Type>::operator=(const stackType<Type>& otherStack)
  61. {
  62. if (this != &otherStack)
  63. copyStack(otherStack);
  64.  
  65. return *this;
  66. }
  67.  
  68. template<class Type>
  69. void stackType<Type>::initializeStack()
  70. {
  71. stackTop = 0;
  72. }
  73.  
  74. template<class Type>
  75. void stackType<Type>::destroyStack()
  76. {
  77. stackTop = 0;
  78. }
  79.  
  80. template<class Type>
  81. bool stackType<Type>::isEmptyStack()
  82. {
  83. return(stackTop == maxStackSize);
  84. }
  85.  
  86. template<class Type>
  87. bool stackType<Type>::isFullStack()
  88. {
  89. return(stackTop == maxStackSize);
  90. }
  91.  
  92. template<class Type>
  93. void stackType<Type>::push(const Type& newItem)
  94. {
  95. if(!isFullStack())
  96. {
  97. list[stackTop] = newItem;
  98.  
  99. stackTop++;
  100. }
  101.  
  102. else
  103. cerr<<"Cannot add to a full stack."<<endl;
  104. }
  105.  
  106. template<class Type>
  107. Type stackType<Type>::top()
  108. {
  109. assert(stackTop != 0);
  110.  
  111. return list[stackTop - 1];
  112. }
  113.  
  114. template<class Type>
  115. void stackType<Type>::pop()
  116. {
  117. if(!isEmptyStack())
  118. {
  119. stackTop--;
  120. }
  121. else
  122. cerr<<"Cannot remove from an empty stack."<<endl;
  123. }
  124.  
  125. template<class Type>
  126. stackType<Type>::stackType(int stackSize)
  127. {
  128. if(stackSize <= 0)
  129. {
  130. cerr<<"The size of the array to hold the stack must "
  131. <<"be positive."<<endl;
  132. cerr<<"Creating an array of size 100."<<endl;
  133.  
  134. maxStackSize = 100;
  135. }
  136.  
  137. else
  138. maxStackSize = stackSize;
  139.  
  140. stackTop = 0;
  141.  
  142. list = new Type[maxStackSize];
  143. }
  144.  
  145. template<class Type>
  146. stackType<Type>::~stackType()
  147. {
  148. delete [] list;
  149. }
  150.  
  151.  
  152. #endif
Last edited by NinjaLink; 33 Days Ago at 10:11 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,813
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster
 
0
  #2
33 Days Ago
Pretend you're the compiler. What does the author mean by "two objects are the same"? All their data members have the same value? Possibly, but there could be other definitions too. You'd have no idea, so you'd throw up your hands and quit. The compiler knows what == and != means for integers, strings, etc., but those aren't custom classes. Yours is. You need to write == and != operators.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,824
Reputation: ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all 
Solved Threads: 117
ithelp's Avatar
ithelp ithelp is offline Offline
Posting Virtuoso
 
0
  #3
33 Days Ago
Pop elements from both stacks and compare whether they are equal.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 365
Reputation: NinjaLink is an unknown quantity at this point 
Solved Threads: 0
NinjaLink NinjaLink is offline Offline
Posting Whiz
 
0
  #4
33 Days Ago
I am trying to create the functions for operator "==" and "!=". I am currently getting an error below: Any help is appreciated.


error:155 bool operator==(const stackType<Type>&)' must take exactly two arguments


But I dont really know what other arguement to take.


  1. #ifndef H_StackType
  2. #define H_StackType
  3.  
  4. #include <iostream>
  5. #include <cassert>
  6.  
  7. using namespace std;
  8.  
  9. template<class Type>
  10.  
  11. class stackType
  12. {
  13.  
  14. public:
  15. const stackType<Type>& operator=(const stackType<Type>&);
  16. void initializeStack();
  17. bool isEmptyStack();
  18. bool isFullStack();
  19. void destroyStack();
  20. void push(const Type& newItem);
  21. Type top();
  22. void pop();
  23. stackType(int stackSize = 100);
  24. stackType(const stackType<Type>& otherStack);
  25. ~stackType();
  26. bool operator==(const stackType<Type>& otherStack);
  27. bool operator!=(const stackType<Type>& otherStack);
  28.  
  29.  
  30. private:
  31. int maxStackSize;
  32. int stackTop;
  33. Type *list;
  34. void copyStack(const stackType<Type>& otherStack);
  35.  
  36. };
  37.  
  38. template<class Type>
  39. void stackType<Type>::copyStack(const stackType<Type>& otherStack)
  40. {
  41. delete [] list;
  42. maxStackSize = otherStack.maxStackSize;
  43. stackTop = otherStack.stackTop;
  44.  
  45. list = new Type[maxStackSize];
  46. assert(list != NULL);
  47.  
  48. for(int j = 0; j < stackTop; j++)
  49. list[j] = otherStack.list[j];
  50.  
  51. }
  52.  
  53. template<class Type>
  54. stackType<Type>::stackType(const stackType<Type>& otherStack)
  55. {
  56. list = NULL;
  57.  
  58. copyStack(otherStack);
  59.  
  60. }
  61.  
  62. template<class Type>
  63. const stackType<Type>& stackType<Type>::operator=(const stackType<Type>& otherStack)
  64. {
  65. if (this != &otherStack)
  66. copyStack(otherStack);
  67.  
  68. return *this;
  69. }
  70.  
  71. template<class Type>
  72. void stackType<Type>::initializeStack()
  73. {
  74. stackTop = 0;
  75. }
  76.  
  77. template<class Type>
  78. void stackType<Type>::destroyStack()
  79. {
  80. stackTop = 0;
  81. }
  82.  
  83. template<class Type>
  84. bool stackType<Type>::isEmptyStack()
  85. {
  86. return(stackTop == maxStackSize);
  87. }
  88.  
  89. template<class Type>
  90. bool stackType<Type>::isFullStack()
  91. {
  92. return(stackTop == maxStackSize);
  93. }
  94.  
  95. template<class Type>
  96. void stackType<Type>::push(const Type& newItem)
  97. {
  98. if(!isFullStack())
  99. {
  100. list[stackTop] = newItem;
  101.  
  102. stackTop++;
  103. }
  104.  
  105. else
  106. cerr<<"Cannot add to a full stack."<<endl;
  107. }
  108.  
  109. template<class Type>
  110. Type stackType<Type>::top()
  111. {
  112. assert(stackTop != 0);
  113.  
  114. return list[stackTop - 1];
  115. }
  116.  
  117. template<class Type>
  118. void stackType<Type>::pop()
  119. {
  120. if(!isEmptyStack())
  121. {
  122. stackTop--;
  123. }
  124. else
  125. cerr<<"Cannot remove from an empty stack."<<endl;
  126. }
  127.  
  128. template<class Type>
  129. stackType<Type>::stackType(int stackSize)
  130. {
  131. if(stackSize <= 0)
  132. {
  133. cerr<<"The size of the array to hold the stack must "
  134. <<"be positive."<<endl;
  135. cerr<<"Creating an array of size 100."<<endl;
  136.  
  137. maxStackSize = 100;
  138. }
  139.  
  140. else
  141. maxStackSize = stackSize;
  142.  
  143. stackTop = 0;
  144.  
  145. list = new Type[maxStackSize];
  146. }
  147.  
  148. template<class Type>
  149. stackType<Type>::~stackType()
  150. {
  151. delete [] list;
  152. }
  153.  
  154. template<class Type>
  155. bool operator== (const stackType<Type>& otherStack)
  156. {
  157. bool eq=true;
  158. }
  159.  
  160. template<class Type>
  161. bool operator!= (const stackType<Type>& otherStack)
  162. {
  163.  
  164. return !( this == otherStack );
  165.  
  166. }
  167.  
  168.  
  169. #endif
Last edited by NinjaLink; 33 Days Ago at 12:35 pm.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,813
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster
 
0
  #5
33 Days Ago
If you've already written an = operator that compiles, but your == and != operators don't compile, you should look at your = operator and see what you did correctly there that you did incorrectly with your == and != operators.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 365
Reputation: NinjaLink is an unknown quantity at this point 
Solved Threads: 0
NinjaLink NinjaLink is offline Offline
Posting Whiz
 
0
  #6
33 Days Ago
Thank you for replying Vernon Dozier.

Is it still necessary to use bool for operator== and operator!= or should i declare the function exactly the same as how I did operator=?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,813
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster
 
0
  #7
33 Days Ago
Originally Posted by NinjaLink View Post
Thank you for replying Vernon Dozier.

Is it still necessary to use bool for operator== and operator!= or should i declare the function exactly the same as how I did operator=?
If it was exactly the same, it wouldn't make any sense and it would be unnecessary. Why have two binary operators that do exactly the same thing? == and != always return boolean values. The point is that you already wrote a binary operator that compiled successfully, so when you need to write another binary operator (==), use the one you already wrote as a skeleton and change as needed.

const stackType<Type>& stackType<Type>::operator=(const stackType<Type>& otherStack)

Now look at your == operator:

  1. bool operator== (const stackType<Type>& otherStack)

See red on the first one and lack of red on the second one. How is the compiler to know that the == operator implementation has anything to do with the StackType class type unless you tell it, like you did with the = operator?
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 365
Reputation: NinjaLink is an unknown quantity at this point 
Solved Threads: 0
NinjaLink NinjaLink is offline Offline
Posting Whiz
 
0
  #8
33 Days Ago
The reason I asked was because when I made the change:


  1. bool stackType<Type>& operator==(bool stackType<Type>&);
  2. bool stackType<Type>& operator!= (bool stackType<Type>&);
  3.  
  4.  
  5. template<class Type>
  6. bool stackType<Type>::operator==(bool stackType<Type>& otherStack)
  7. {
  8. bool eq=true;
  9. }
  10.  
  11. template<class Type>
  12. bool stackType<Type>::operator!=(bool stackType<Type>& otherStack)
  13. {
  14.  
  15. return !(this == &otherStack);
  16.  
  17. }


I received these errors in the header file:

26 `stackType<Type>' specified as declarator-id
26 perhaps you want `stackType<Type>' for a constructor
26 `bool' is now a keyword
26 expected `;' before '&' token
27 `stackType<Type>' specified as declarator-id
27 perhaps you want `stackType<Type>' for a constructor
27 `bool' is now a keyword
27 expected `;' before '&' token
155 expected unqualified-id before '&' token
155 expected `,' or `...' before '&' token
156 no `bool stackType<Type>::operator==(bool)' member function declared in class `stackType<Type>'
156 template definition of non-template `bool stackType<Type>::operator==(bool)'




I don't know if this has anything to do with the changes that I made or the contents that I have in my function so far.


header file:



  1. #ifndef H_StackType
  2. #define H_StackType
  3.  
  4. #include <iostream>
  5. #include <cassert>
  6.  
  7. using namespace std;
  8.  
  9. template<class Type>
  10.  
  11. class stackType
  12. {
  13.  
  14. public:
  15. const stackType<Type>& operator=(const stackType<Type>&);
  16. void initializeStack();
  17. bool isEmptyStack();
  18. bool isFullStack();
  19. void destroyStack();
  20. void push(const Type& newItem);
  21. Type top();
  22. void pop();
  23. stackType(int stackSize = 100);
  24. stackType(const stackType<Type>& otherStack);
  25. ~stackType();
  26. bool stackType<Type>& operator==(bool stackType<Type>&);
  27. bool stackType<Type>& operator!= (bool stackType<Type>&);
  28.  
  29.  
  30. private:
  31. int maxStackSize;
  32. int stackTop;
  33. Type *list;
  34. void copyStack(const stackType<Type>& otherStack);
  35.  
  36. };
  37.  
  38. template<class Type>
  39. void stackType<Type>::copyStack(const stackType<Type>& otherStack)
  40. {
  41. delete [] list;
  42. maxStackSize = otherStack.maxStackSize;
  43. stackTop = otherStack.stackTop;
  44.  
  45. list = new Type[maxStackSize];
  46. assert(list != NULL);
  47.  
  48. for(int j = 0; j < stackTop; j++)
  49. list[j] = otherStack.list[j];
  50.  
  51. }
  52.  
  53. template<class Type>
  54. stackType<Type>::stackType(const stackType<Type>& otherStack)
  55. {
  56. list = NULL;
  57.  
  58. copyStack(otherStack);
  59.  
  60. }
  61.  
  62. template<class Type>
  63. const stackType<Type>& stackType<Type>::operator=(const stackType<Type>& otherStack)
  64. {
  65. if (this != &otherStack)
  66. copyStack(otherStack);
  67.  
  68. return *this;
  69. }
  70.  
  71. template<class Type>
  72. void stackType<Type>::initializeStack()
  73. {
  74. stackTop = 0;
  75. }
  76.  
  77. template<class Type>
  78. void stackType<Type>::destroyStack()
  79. {
  80. stackTop = 0;
  81. }
  82.  
  83. template<class Type>
  84. bool stackType<Type>::isEmptyStack()
  85. {
  86. return(stackTop == maxStackSize);
  87. }
  88.  
  89. template<class Type>
  90. bool stackType<Type>::isFullStack()
  91. {
  92. return(stackTop == maxStackSize);
  93. }
  94.  
  95. template<class Type>
  96. void stackType<Type>::push(const Type& newItem)
  97. {
  98. if(!isFullStack())
  99. {
  100. list[stackTop] = newItem;
  101.  
  102. stackTop++;
  103. }
  104.  
  105. else
  106. cerr<<"Cannot add to a full stack."<<endl;
  107. }
  108.  
  109. template<class Type>
  110. Type stackType<Type>::top()
  111. {
  112. assert(stackTop != 0);
  113.  
  114. return list[stackTop - 1];
  115. }
  116.  
  117. template<class Type>
  118. void stackType<Type>::pop()
  119. {
  120. if(!isEmptyStack())
  121. {
  122. stackTop--;
  123. }
  124. else
  125. cerr<<"Cannot remove from an empty stack."<<endl;
  126. }
  127.  
  128. template<class Type>
  129. stackType<Type>::stackType(int stackSize)
  130. {
  131. if(stackSize <= 0)
  132. {
  133. cerr<<"The size of the array to hold the stack must "
  134. <<"be positive."<<endl;
  135. cerr<<"Creating an array of size 100."<<endl;
  136.  
  137. maxStackSize = 100;
  138. }
  139.  
  140. else
  141. maxStackSize = stackSize;
  142.  
  143. stackTop = 0;
  144.  
  145. list = new Type[maxStackSize];
  146. }
  147.  
  148. template<class Type>
  149. stackType<Type>::~stackType()
  150. {
  151. delete [] list;
  152. }
  153.  
  154. template<class Type>
  155. bool stackType<Type>::operator==(bool stackType<Type>& otherStack)
  156. {
  157. bool eq=true;
  158. }
  159.  
  160. template<class Type>
  161. bool stackType<Type>::operator!=(bool stackType<Type>& otherStack)
  162. {
  163.  
  164. return !(this == &otherStack);
  165.  
  166. }
  167.  
  168.  
  169. #endif
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,813
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster
 
0
  #9
33 Days Ago
Go back to post 4. Change lines 154 - 166 to this:

  1. template<class Type>
  2. bool stackType<Type>::operator== (const stackType<Type>& otherStack)
  3. {
  4. bool eq=true;
  5. }
  6.  
  7. template<class Type>
  8. bool stackType<Type>::operator!= (const stackType<Type>& otherStack)
  9. {
  10. return !( this == otherStack );
  11. }

All you want to do is add stackType<Type>:: before the word operator . Everything else stays the same except you do need to change the contents of your == function too since lines 156 - 158 don't do anything and you have no return statement. But your compiler problem goes away if you make the changes above.

[EDIT]
Well, they don't all go away necessarily, but the linker problems do. Just have == and != return true and false for now, get it to compile, then fix ==, then after that's done, worry about !=.
[/EDIT]
Last edited by VernonDozier; 33 Days Ago at 2:58 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 365
Reputation: NinjaLink is an unknown quantity at this point 
Solved Threads: 0
NinjaLink NinjaLink is offline Offline
Posting Whiz
 
0
  #10
33 Days Ago
Thank you Vernon Dozier.

I was working on the program some more and corrected the issues and started populating my operator== and operator!= to try to get things working. However, I came across these 2 errors that I need some assistance on. Hope you can help!


Errors:

12 In member function `bool stackType<Type>::operator==(const stackType<Type>&) [with Type = int]': instantiated from here

158 passing `const stackType<int>' as `this' argument of `bool stackType<Type>::isFullStack() [with Type = int]' discards qualifiers



header file:


  1. #ifndef H_StackType
  2. #define H_StackType
  3.  
  4. #include <iostream>
  5. #include <cassert>
  6.  
  7. using namespace std;
  8.  
  9. template<class Type>
  10.  
  11. class stackType
  12. {
  13.  
  14. public:
  15. const stackType<Type>& operator=(const stackType<Type>&);
  16. void initializeStack();
  17. bool isEmptyStack();
  18. bool isFullStack();
  19. void destroyStack();
  20. void push(const Type& newItem);
  21. Type top();
  22. void pop();
  23. stackType(int stackSize = 100);
  24. stackType(const stackType<Type>& otherStack);
  25. ~stackType();
  26. bool operator== (const stackType<Type>&);
  27. bool operator!= (const stackType<Type>&);
  28.  
  29.  
  30. private:
  31. int maxStackSize;
  32. int stackTop;
  33. Type *list;
  34. void copyStack(const stackType<Type>& otherStack);
  35.  
  36. };
  37.  
  38. template<class Type>
  39. void stackType<Type>::copyStack(const stackType<Type>& otherStack)
  40. {
  41. delete [] list;
  42. maxStackSize = otherStack.maxStackSize;
  43. stackTop = otherStack.stackTop;
  44.  
  45. list = new Type[maxStackSize];
  46. assert(list != NULL);
  47.  
  48. for(int j = 0; j < stackTop; j++)
  49. list[j] = otherStack.list[j];
  50.  
  51. }
  52.  
  53. template<class Type>
  54. stackType<Type>::stackType(const stackType<Type>& otherStack)
  55. {
  56. list = NULL;
  57.  
  58. copyStack(otherStack);
  59.  
  60. }
  61.  
  62. template<class Type>
  63. const stackType<Type>& stackType<Type>::operator=(const stackType<Type>& otherStack)
  64. {
  65. if (this != &otherStack)
  66. copyStack(otherStack);
  67.  
  68. return *this;
  69. }
  70.  
  71. template<class Type>
  72. void stackType<Type>::initializeStack()
  73. {
  74. stackTop = 0;
  75. }
  76.  
  77. template<class Type>
  78. void stackType<Type>::destroyStack()
  79. {
  80. stackTop = 0;
  81. }
  82.  
  83. template<class Type>
  84. bool stackType<Type>::isEmptyStack()
  85. {
  86. return(stackTop == maxStackSize);
  87. }
  88.  
  89. template<class Type>
  90. bool stackType<Type>::isFullStack()
  91. {
  92. return(stackTop == maxStackSize);
  93. }
  94.  
  95. template<class Type>
  96. void stackType<Type>::push(const Type& newItem)
  97. {
  98. if(!isFullStack())
  99. {
  100. list[stackTop] = newItem;
  101.  
  102. stackTop++;
  103. }
  104.  
  105. else
  106. cerr<<"Cannot add to a full stack."<<endl;
  107. }
  108.  
  109. template<class Type>
  110. Type stackType<Type>::top()
  111. {
  112. assert(stackTop != 0);
  113.  
  114. return list[stackTop - 1];
  115. }
  116.  
  117. template<class Type>
  118. void stackType<Type>::pop()
  119. {
  120. if(!isEmptyStack())
  121. {
  122. stackTop--;
  123. }
  124. else
  125. cerr<<"Cannot remove from an empty stack."<<endl;
  126. }
  127.  
  128. template<class Type>
  129. stackType<Type>::stackType(int stackSize)
  130. {
  131. if(stackSize <= 0)
  132. {
  133. cerr<<"The size of the array to hold the stack must "
  134. <<"be positive."<<endl;
  135. cerr<<"Creating an array of size 100."<<endl;
  136.  
  137. maxStackSize = 100;
  138. }
  139.  
  140. else
  141. maxStackSize = stackSize;
  142.  
  143. stackTop = 0;
  144.  
  145. list = new Type[maxStackSize];
  146. }
  147.  
  148. template<class Type>
  149. stackType<Type>::~stackType()
  150. {
  151. delete [] list;
  152. }
  153.  
  154. template<class Type>
  155. bool stackType<Type>::operator== (const stackType<Type>& otherStack)
  156. {
  157.  
  158. if (otherStack.isFullStack() != isFullStack())
  159.  
  160. cerr<<"They are not the same size"<<endl;
  161.  
  162. for(int j = 0 ; j < isFullStack(); ++j)
  163. {
  164. if (otherStack.list[j] != list[j])
  165.  
  166. return false;
  167.  
  168. else
  169.  
  170. return true;
  171. }
  172.  
  173. }
  174.  
  175. template<class Type>
  176. bool stackType<Type>::operator!= (const stackType<Type>& otherStack)
  177. {
  178.  
  179. return !(*this == otherStack);
  180.  
  181. }
  182.  
  183.  
  184.  
  185. #endif




main.cpp


  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include "stack.h"
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. stackType<int> s1;
  10. stackType<int> s2;
  11.  
  12. if (s1 == s2)
  13. {
  14. cout << "empty stacks are equal" << endl;
  15. }
  16. s1.push(10); s1.push(20);
  17. s2.push(10);
  18. cout << "s1: ";
  19. cout << "s2: ";
  20. cout << "s1 ";
  21. if (s1 != s2)
  22. cout << "!";
  23. cout << "= s2" << endl;
  24. s2.push(20);
  25. cout << "s2: ";
  26. cout << "s1 ";
  27. if (s1 == s2)
  28. {
  29. cout << "is equal ";
  30. } else {
  31. cout << "is not equal " << endl;
  32. }
  33. cout << "s2" << endl;
  34. s1.pop(); s1.pop();
  35. s1.push(30); s1.push(20);
  36. cout << "s1: ";
  37. cout << "s1 ";
  38. if (s1 != s2)
  39. cout << " is";
  40. cout << " not equal to s2" << endl;
  41.  
  42. system ("pause");
  43. return 0;
  44. }
Reply With Quote Quick reply to this message  
Reply

Message:


Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC