C++ operator== not working properly

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++ operator== function not working properly

 
0
  #1
33 Days Ago
I had another thread before this one, but I felt that it was getting too long.

Current problem: My operator== is not working properly in my program. I have done some things in my function, but I don't think it is "completely right". My program compiles fine and the output is displayed, but whenever I do something like (s1 == s2), it gives the wrong result like "The 2 stacks are NOT equal). Will someone please help? I am stuck. My whole program is listed below:

Current Output:

  1. s1:
  2. 40
  3. 30
  4. 20
  5. 10
  6.  
  7. s2:
  8. 50
  9. 30
  10. 20
  11. 10
  12.  
  13. The 2 stacks are NOT equal
  14.  
  15. s1:
  16. 20
  17. 15
  18. 10
  19. 5
  20.  
  21. s2:
  22. 20
  23. 15
  24. 10
  25. 5
  26.  
  27. The 2 stacks are NOT equal
  28.  
  29.  
  30. s1:
  31. 8
  32. 6
  33. 4
  34. 2
  35.  
  36. s2:
  37. 12
  38. 9
  39. 6
  40. 3
  41.  
  42. The 2 stacks are NOT equal



Targeted Functions:

  1.  
  2. template<class Type>
  3. bool stackType<Type>::isEqual(const stackType<Type>& otherStack)
  4. {
  5. bool bRet = false;
  6.  
  7. if (otherStack.maxStackSize == maxStackSize && otherStack.stackTop == stackTop)
  8. {
  9.  
  10. bRet = true;
  11.  
  12. for (int j = 0; j < stackTop; ++j)
  13. {
  14.  
  15. if (otherStack.list[j] != list[j])
  16. {
  17. // cout << "!=( " << j << ") " << otherStack.list[j] << "!=" << list[j];
  18. bRet = false;
  19. break;
  20. }
  21. }
  22. }
  23. return bRet;
  24. }
  25.  
  26. template<class Type>
  27. bool stackType<Type>::operator==(const stackType<Type>& otherStack)
  28. {
  29. return isEqual(otherStack);
  30. }
  31.  
  32. template<class Type>
  33. bool stackType<Type>::operator!=(const stackType<Type>& otherStack)
  34. {
  35. return !isEqual(otherStack); //!(*this == otherStack);
  36. }






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.  
  13. s1.push(10); s1.push(20); s1.push(30); s1.push(40);
  14. s2.push(10); s2.push(20); s2.push(30); s2.push(50);
  15.  
  16. cout << "s1: "<<endl; s1.printStack(); cout<<endl;
  17.  
  18. cout << "s2: "<<endl; s2.printStack(); cout<<endl;
  19.  
  20. if (s1 == s2)
  21. {
  22. cout<<"The 2 stacks are equal"<<endl;
  23. }
  24. if (s1 != s2)
  25. {
  26. cout<<"The 2 stacks are NOT equal"<<endl;
  27. }
  28.  
  29. cout <<endl;
  30.  
  31.  
  32. s1.push(5); s1.push(10); s1.push(15); s1.push(20);
  33. s2.push(5); s2.push(10); s2.push(15); s2.push(20);
  34.  
  35. cout << "s1: "<<endl; s1.printStack(); cout<<endl;
  36. cout << "s2: "<<endl; s2.printStack(); cout<<endl;
  37.  
  38. if (s1 == s2)
  39. {
  40. cout << "The 2 stacks are equal";
  41. }
  42. if (s1 != s2)
  43. {
  44. cout << "The 2 stacks are NOT equal" << endl;
  45. }
  46.  
  47. cout<<endl;
  48. cout<<endl;
  49.  
  50. s1.push(2); s1.push(4); s1.push(6); s1.push(8);
  51. s2.push(3); s2.push(6); s2.push(9); s2.push(12);
  52.  
  53. cout << "s1: "<<endl; s1.printStack(); cout<<endl;
  54. cout << "s2: "<<endl; s2.printStack(); cout<<endl;
  55.  
  56. if (s1 == s2)
  57. {
  58. cout << "The 2 stacks are equal";
  59. }
  60. else
  61. {
  62. cout << "The 2 stacks are NOT equal" << endl;
  63. }
  64.  
  65. cout<<endl;
  66. cout<<endl;
  67.  
  68. system("PAUSE");
  69. return 0;
  70. }




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 printStack();
  21. void push(const Type& newItem);
  22. Type top();
  23. void pop();
  24. stackType(int stackSize = 100);
  25. stackType(const stackType<Type>& otherStack);
  26. ~stackType();
  27. bool operator== (const stackType<Type>&);
  28. bool operator!= (const stackType<Type>&);
  29.  
  30.  
  31.  
  32.  
  33. private:
  34. int maxStackSize;
  35. int stackTop;
  36. Type *list;
  37. void copyStack(const stackType<Type>& otherStack);
  38. bool isEqual(const stackType<Type>&);
  39.  
  40. };
  41.  
  42. template<class Type>
  43. void stackType<Type>::copyStack(const stackType<Type>& otherStack)
  44. {
  45. delete [] list;
  46. maxStackSize = otherStack.maxStackSize;
  47. stackTop = otherStack.stackTop;
  48.  
  49. list = new Type[maxStackSize];
  50. assert(list != NULL);
  51.  
  52. for(int j = 0; j < stackTop; j++)
  53. list[j] = otherStack.list[j];
  54.  
  55. }
  56.  
  57. template<class Type>
  58. stackType<Type>::stackType(const stackType<Type>& otherStack)
  59. {
  60. list = NULL;
  61.  
  62. copyStack(otherStack);
  63.  
  64. }
  65.  
  66. template<class Type>
  67. const stackType<Type>& stackType<Type>::operator=(const stackType<Type>& otherStack)
  68. {
  69. if (this != &otherStack)
  70. copyStack(otherStack);
  71.  
  72. return *this;
  73. }
  74.  
  75. template<class Type>
  76. void stackType<Type>::initializeStack()
  77. {
  78. stackTop = 0;
  79. }
  80.  
  81. template<class Type>
  82. void stackType<Type>::destroyStack()
  83. {
  84. stackTop = 0;
  85. }
  86.  
  87. template<class Type>
  88. bool stackType<Type>::isEmptyStack()
  89. {
  90. return(stackTop == 0);
  91. }
  92.  
  93. template<class Type>
  94. bool stackType<Type>::isFullStack()
  95. {
  96. return(stackTop == maxStackSize);
  97. }
  98.  
  99. template<class Type>
  100. void stackType<Type>::push(const Type& newItem)
  101. {
  102. if(!isFullStack())
  103. {
  104. list[stackTop] = newItem;
  105.  
  106. stackTop++;
  107. }
  108.  
  109. else
  110. cerr<<"Cannot add to a full stack."<<endl;
  111. }
  112.  
  113. template<class Type>
  114. Type stackType<Type>::top()
  115. {
  116. assert(stackTop != 0);
  117.  
  118. return list[stackTop - 1];
  119. }
  120.  
  121. template<class Type>
  122. void stackType<Type>::pop()
  123. {
  124. if(!isEmptyStack())
  125. {
  126. stackTop--;
  127. }
  128. else
  129. cerr<<"Cannot remove from an empty stack."<<endl;
  130. }
  131.  
  132. template<class Type>
  133. stackType<Type>::stackType(int stackSize)
  134. {
  135. if(stackSize <= 0)
  136. {
  137. cerr<<"The size of the array to hold the stack must "
  138. <<"be positive."<<endl;
  139. cerr<<"Creating an array of size 100."<<endl;
  140.  
  141. maxStackSize = 100;
  142. }
  143.  
  144. else
  145. maxStackSize = stackSize;
  146.  
  147. stackTop = 0;
  148.  
  149. list = new Type[maxStackSize];
  150. }
  151.  
  152. template<class Type>
  153. stackType<Type>::~stackType()
  154. {
  155. delete [] list;
  156. }
  157.  
  158.  
  159.  
  160. template<class Type>
  161. bool stackType<Type>::isEqual(const stackType<Type>& otherStack)
  162. {
  163. bool bRet = false;
  164.  
  165. if (otherStack.maxStackSize == maxStackSize && otherStack.stackTop == stackTop)
  166. {
  167. bRet = true;
  168. for (int j = 0; j < stackTop; ++j)
  169. {
  170. if (otherStack.list[j] != list[j])
  171. {
  172. // cout << "!=( " << j << ") " << otherStack.list[j] << "!=" << list[j];
  173. bRet = false;
  174. break;
  175. }
  176. }
  177. }
  178. return bRet;
  179. }
  180.  
  181. template<class Type>
  182. bool stackType<Type>::operator==(const stackType<Type>& otherStack) {
  183. return isEqual(otherStack);
  184. }
  185.  
  186. template<class Type>
  187. bool stackType<Type>::operator!=(const stackType<Type>& otherStack) {
  188. return !isEqual(otherStack); //!(*this == otherStack);
  189. }
  190.  
  191.  
  192. template<class Type>
  193. void stackType<Type>::printStack()
  194. {
  195.  
  196. while (!isEmptyStack())
  197. {
  198.  
  199. cout<<top()<<endl;
  200. pop();
  201.  
  202. }
  203.  
  204.  
  205. }
  206.  
  207.  
  208.  
  209. #endif
Last edited by NinjaLink; 33 Days Ago at 3:08 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 132
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster
 
0
  #2
33 Days Ago
I get all stacks being equal because you call printStack() on both operands and printStack() pops the contents away. Both stacks are always empty. When doing these tests, make sure that you are clearing out the stack at the right time.
-Tommy (For Great Justice!) Gunn
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
  #3
33 Days Ago
Does this mean that my operator== function works fine and I just need to reorganizae my main.cpp?
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 132
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster
 
0
  #4
32 Days Ago
Does this mean that my operator== function works fine and I just need to reorganizae my main.cpp?
Precisely. I cannot say for sure that your isEqual() function is working as designed without a thorough test, but my eyeball test did not expose any glaring bugs.
-Tommy (For Great Justice!) Gunn
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
  #5
32 Days Ago
I don't understand why it doesn't work because I have tried several different ways and spent several hours on doing the IsEqual, operator==, and operator!= functions. Is there any other hints or ideas on why it is not working? because I am stuck and don't know what to do.



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.  
  13. s1.push(10); s1.push(20); s1.push(30); s1.push(40);
  14. s2.push(10); s2.push(20); s2.push(30); s2.push(50);
  15. cout << "s1: "<<endl; s1.printStack(); cout<<endl;
  16. cout << "s2: "<<endl; s2.printStack(); cout<<endl;
  17.  
  18. if (s1 == s2)
  19.  
  20. cout<<"The 2 stacks are equal"<<endl;
  21. else
  22.  
  23. cout<<"The 2 stacks are n equal"<<endl;
  24.  
  25.  
  26. cout <<endl;
  27.  
  28.  
  29. s1.push(5); s1.push(10); s1.push(15); s1.push(20);
  30. s2.push(5); s2.push(10); s2.push(15); s2.push(20);
  31.  
  32. cout << "s1: "<<endl; s1.printStack(); cout<<endl;
  33. cout << "s2: "<<endl; s2.printStack(); cout<<endl;
  34.  
  35. if (s1 == s2)
  36. {
  37. cout << "The 2 stacks are equal";
  38. }
  39. else
  40. {
  41. cout << "The 2 stacks are NOT equal" << endl;
  42. }
  43.  
  44. cout<<endl;
  45. cout<<endl;
  46.  
  47. s1.push(2); s1.push(4); s1.push(6); s1.push(8);
  48. s2.push(3); s2.push(6); s2.push(9); s2.push(12);
  49.  
  50. cout << "s1: "<<endl; s1.printStack(); cout<<endl;
  51. cout << "s2: "<<endl; s2.printStack(); cout<<endl;
  52.  
  53. if (s1 == s2)
  54. {
  55. cout << "The 2 stacks are equal";
  56. }
  57. else
  58. {
  59. cout << "The 2 stacks are NOT equal" << endl;
  60. }
  61.  
  62. cout<<endl;
  63. cout<<endl;
  64.  
  65. system("PAUSE");
  66. return 0;
  67. }








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 printStack();
  21. void push(const Type& newItem);
  22. Type top();
  23. void pop();
  24. stackType(int stackSize = 100);
  25. stackType(const stackType<Type>& otherStack);
  26. ~stackType();
  27. bool operator== (const stackType<Type>&);
  28. bool operator!= (const stackType<Type>&);
  29.  
  30.  
  31.  
  32.  
  33. private:
  34. int maxStackSize;
  35. int stackTop;
  36. Type *list;
  37. void copyStack(const stackType<Type>& otherStack);
  38. bool isEqual(const stackType<Type>&);
  39.  
  40. };
  41.  
  42. template<class Type>
  43. void stackType<Type>::copyStack(const stackType<Type>& otherStack)
  44. {
  45. delete [] list;
  46. maxStackSize = otherStack.maxStackSize;
  47. stackTop = otherStack.stackTop;
  48.  
  49. list = new Type[maxStackSize];
  50. assert(list != NULL);
  51.  
  52. for(int j = 0; j < stackTop; j++)
  53. list[j] = otherStack.list[j];
  54.  
  55. }
  56.  
  57. template<class Type>
  58. stackType<Type>::stackType(const stackType<Type>& otherStack)
  59. {
  60. list = NULL;
  61.  
  62. copyStack(otherStack);
  63.  
  64. }
  65.  
  66. template<class Type>
  67. const stackType<Type>& stackType<Type>::operator=(const stackType<Type>& otherStack)
  68. {
  69. if (this != &otherStack)
  70. copyStack(otherStack);
  71.  
  72. return *this;
  73. }
  74.  
  75. template<class Type>
  76. void stackType<Type>::initializeStack()
  77. {
  78. stackTop = 0;
  79. }
  80.  
  81. template<class Type>
  82. void stackType<Type>::destroyStack()
  83. {
  84. stackTop = 0;
  85. }
  86.  
  87. template<class Type>
  88. bool stackType<Type>::isEmptyStack()
  89. {
  90. return(stackTop == 0);
  91. }
  92.  
  93. template<class Type>
  94. bool stackType<Type>::isFullStack()
  95. {
  96. return(stackTop == maxStackSize);
  97. }
  98.  
  99. template<class Type>
  100. void stackType<Type>::push(const Type& newItem)
  101. {
  102. if(!isFullStack())
  103. {
  104. list[stackTop] = newItem;
  105.  
  106. stackTop++;
  107. }
  108.  
  109. else
  110. cerr<<"Cannot add to a full stack."<<endl;
  111. }
  112.  
  113. template<class Type>
  114. Type stackType<Type>::top()
  115. {
  116. assert(stackTop != 0);
  117.  
  118. return list[stackTop - 1];
  119. }
  120.  
  121. template<class Type>
  122. void stackType<Type>::pop()
  123. {
  124. if(!isEmptyStack())
  125. {
  126. stackTop--;
  127. }
  128. else
  129. cerr<<"Cannot remove from an empty stack."<<endl;
  130. }
  131.  
  132. template<class Type>
  133. stackType<Type>::stackType(int stackSize)
  134. {
  135. if(stackSize <= 0)
  136. {
  137. cerr<<"The size of the array to hold the stack must "
  138. <<"be positive."<<endl;
  139. cerr<<"Creating an array of size 100."<<endl;
  140.  
  141. maxStackSize = 100;
  142. }
  143.  
  144. else
  145. maxStackSize = stackSize;
  146.  
  147. stackTop = 0;
  148.  
  149. list = new Type[maxStackSize];
  150. }
  151.  
  152. template<class Type>
  153. stackType<Type>::~stackType()
  154. {
  155. delete [] list;
  156. }
  157.  
  158.  
  159.  
  160. template<class Type>
  161. bool stackType<Type>::isEqual(const stackType<Type>& otherStack)
  162. {
  163. bool bRet = false;
  164.  
  165. if (otherStack.maxStackSize == maxStackSize && otherStack.stackTop == stackTop)
  166. {
  167. bRet = true;
  168. for (int j = 0; j < stackTop; ++j)
  169. {
  170. if (otherStack.list[j] != list[j])
  171. {
  172. // cout << "!=( " << j << ") " << otherStack.list[j] << "!=" << list[j];
  173. bRet = false;
  174. break;
  175. }
  176. }
  177. }
  178. return bRet;
  179. }
  180.  
  181. template<class Type>
  182. bool stackType<Type>::operator==(const stackType<Type>& otherStack) {
  183. return isEqual(otherStack);
  184. }
  185.  
  186. template<class Type>
  187. bool stackType<Type>::operator!=(const stackType<Type>& otherStack) {
  188. return !isEqual(otherStack); //!(*this == otherStack);
  189. }
  190.  
  191.  
  192. template<class Type>
  193. void stackType<Type>::printStack()
  194. {
  195.  
  196. while (!isEmptyStack())
  197. {
  198.  
  199. cout<<top()<<endl;
  200. pop();
  201.  
  202. }
  203.  
  204.  
  205. }
  206.  
  207.  
  208.  
  209. #endif
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 681
Reputation: Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of Tom Gunn has much to be proud of 
Solved Threads: 132
Tom Gunn's Avatar
Tom Gunn Tom Gunn is offline Offline
Practically a Master Poster
 
0
  #6
32 Days Ago
Is there any other hints or ideas on why it is not working? because I am stuck and don't know what to do.
Your printStack() method is wrong because it prints the stack and clears it at the same time. That is problem #1. If you fix that, then your stack objects will not be cleared when they should be for an accurate 3 part test. That is problem #2.

The printStack() method should look more like this if you intend to have it print and not clear. This fixes problem #1:
  1. template<class Type>
  2. void stackType<Type>::printStack()
  3. {
  4. for (int x = stackTop-1; x >= 0; --x)
  5. {
  6. cout << list[x] << '\n';
  7. }
  8. }
You can manually clear the stack objects, but a more self contained test would be better. For example, you can generalize the test into a function and then call it 3 times. This allows the stack objects to be destroyed and recreated with each function call, which fixes problem #2:
  1. #include <iostream>
  2.  
  3. void RunTest(int* values1, int sz1,
  4. int* values2, int sz2)
  5. {
  6. using std::cout;
  7.  
  8. stackType<int> s1;
  9. stackType<int> s2;
  10.  
  11. for (int x = 0; x < sz1; ++x) s1.push(values1[x]);
  12. for (int x = 0; x < sz2; ++x) s2.push(values2[x]);
  13.  
  14. cout << "s1:\n"; s1.printStack(); cout << '\n';
  15. cout << "s2:\n"; s2.printStack(); cout << '\n';
  16.  
  17. if (s1 == s2) cout << "The 2 stacks are equal\n";
  18. if (s1 != s2) cout << "The 2 stacks are NOT equal\n";
  19.  
  20. cout << "\n\n";
  21. }
  22.  
  23. int main()
  24. {
  25. int values1[4] = {10, 20, 30, 40};
  26. int values2[4] = {10, 20, 30, 50};
  27.  
  28. RunTest(values1, 4, values2, 4);
  29.  
  30. values1[0] = 5, values1[1] = 10, values1[2] = 15, values1[3] = 20;
  31. values2[0] = 5, values2[1] = 10, values2[2] = 15, values2[3] = 20;
  32.  
  33. RunTest(values1, 4, values2, 4);
  34.  
  35. values1[0] = 2, values1[1] = 4, values1[2] = 6, values1[3] = 8;
  36. values2[0] = 3, values2[1] = 6, values2[2] = 9, values2[3] = 12;
  37.  
  38. RunTest(values1, 4, values2, 4);
  39. }
It is important to learn how to troubleshoot this kind of problem, or you will end up wasting hours debugging code that is not broken.
Last edited by Tom Gunn; 32 Days Ago at 5:03 pm.
-Tommy (For Great Justice!) Gunn
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