944,129 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 985
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Oct 25th, 2009
0
Re: C++ Compare stacks help!
You need to read these error messages carefully. They tell you exactly what's wrong.

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

The word "qualifier" should pop out at you. Compare the functions. One has "const", one doesn't. "const" is a qualifier. There's a decent chance that you are using the "const" qualifier incorrectly. To find out, go through the file and replace "const" with "/*const*/" everywhere and recompile. if it compiles correctly this time, you know what the problem is for sure, so if you don't yet know how to use "const", put the project aside, learn how, and come back to it. Then fix the "const" problem. Then change the "/*const*/" back to "const" and go from there. The error messages tell you exactly what's wrong. Read them carefully.
Last edited by VernonDozier; Oct 25th, 2009 at 3:57 pm.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Oct 25th, 2009
0
Re: C++ Compare stacks help!
Alright I will try to see if I can figure it out. Does everything else look fine in my operator== and operator!=? or does it still needs work. Thanks
Reputation Points: 8
Solved Threads: 0
Posting Pro in Training
NinjaLink is offline Offline
416 posts
since Mar 2008
Oct 25th, 2009
0
Re: C++ Compare stacks help!
Click to Expand / Collapse  Quote originally posted by NinjaLink ...
Alright I will try to see if I can figure it out. Does everything else look fine in my operator== and operator!=? or does it still needs work. Thanks
== still needs work. You'll need to rewrite it even after you fix the "const" problem(s).
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Oct 26th, 2009
-1
Re: C++ Compare stacks help!
Hey, I finally figured it out even though it took several hours. However, I have one last question. In main.cpp, how do I print out each stack so that it looks like the Expected output below. Thanks!



Current output:

s1:
s2:
s1 != s2
s2:
s1 is equal s2
s1:
s1 is not equal to s2


Expected output:

s1: 20 10
s2: 10
s1 != s2
s2: 20 10
s1 is equal s2
s1: 20 30
s1 is not equal to s2


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



header file:


C++ Syntax (Toggle Plain Text)
  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. bool operator==(const Stack<T> &s1)
  20. bool operator!=(const Stack<T> &s1)
  21. void destroyStack();
  22. void push(const Type& newItem);
  23. Type top();
  24. void pop();
  25. stackType(int stackSize = 100);
  26. stackType(const stackType<Type>& otherStack);
  27. ~stackType();
  28.  
  29.  
  30. private:
  31. int maxStackSize;
  32. int stackTop;
  33. Type *list;
  34. void copyStack(const stackType<Type>& otherStack);
  35. };
  36.  
  37. template<class Type>
  38. void stackType<Type>::copyStack(const stackType<Type>& otherStack)
  39. {
  40. delete [] list;
  41. maxStackSize = otherStack.maxStackSize;
  42. stackTop = otherStack.stackTop;
  43.  
  44. list = new Type[maxStackSize];
  45. assert(list != NULL);
  46.  
  47. for(int j = 0; j < stackTop; j++)
  48. list[j] = otherStack.list[j];
  49.  
  50. }
  51.  
  52. template<class Type>
  53. stackType<Type>::stackType(const stackType<Type>& otherStack)
  54. {
  55. list = NULL;
  56.  
  57. copyStack(otherStack);
  58.  
  59. }
  60.  
  61. template<class Type>
  62. const stackType<Type>& stackType<Type>::operator=(const stackType<Type>& otherStack)
  63. {
  64. if (this != &otherStack)
  65. copyStack(otherStack);
  66.  
  67. return *this;
  68. }
  69.  
  70. template<class Type>
  71. void stackType<Type>::initializeStack()
  72. {
  73. stackTop = 0;
  74. }
  75.  
  76. template<class Type>
  77. void stackType<Type>::destroyStack()
  78. {
  79. stackTop = 0;
  80. }
  81.  
  82. template<class Type>
  83. bool stackType<Type>::isEmptyStack()
  84. {
  85. return(stackTop == maxStackSize);
  86. }
  87.  
  88. template<class Type>
  89. bool stackType<Type>::isFullStack()
  90. {
  91. return(stackTop == maxStackSize);
  92. }
  93.  
  94. template<class Type>
  95. void stackType<Type>::push(const Type& newItem)
  96. {
  97. if(!isFullStack())
  98. {
  99. list[stackTop] = newItem;
  100.  
  101. stackTop++;
  102. }
  103.  
  104. else
  105. cerr<<"Cannot add to a full stack."<<endl;
  106. }
  107.  
  108. template<class Type>
  109. Type stackType<Type>::top()
  110. {
  111. assert(stackTop != 0);
  112.  
  113. return list[stackTop - 1];
  114. }
  115.  
  116. template<class Type>
  117. void stackType<Type>::pop()
  118. {
  119. if(!isEmptyStack())
  120. {
  121. stackTop--;
  122. }
  123. else
  124. cerr<<"Cannot remove from an empty stack."<<endl;
  125. }
  126.  
  127. template<class Type>
  128. stackType<Type>::stackType(int stackSize)
  129. {
  130. if(stackSize <= 0)
  131. {
  132. cerr<<"The size of the array to hold the stack must "
  133. <<"be positive."<<endl;
  134. cerr<<"Creating an array of size 100."<<endl;
  135.  
  136. maxStackSize = 100;
  137. }
  138.  
  139. else
  140. maxStackSize = stackSize;
  141.  
  142. stackTop = 0;
  143.  
  144. list = new Type[maxStackSize];
  145. }
  146.  
  147. template<class Type>
  148. stackType<Type>::~stackType()
  149. {
  150. delete [] list;
  151. }
  152.  
  153.  
  154. bool operator==(const Stack<T> &s1) {
  155. bool eq = true;
  156.  
  157. if (s1.stackLen() != this->stackLen()) {
  158. eq = false;
  159. } else if (s1.empty() == false) {
  160. size_t len = this->stackLen();
  161. T *p = this->_top;
  162. T *q = s1._top;
  163. for (size_t i = 0; (i < len) && (eq == true); i++) {
  164. eq = (*p++ == *q++);
  165. }
  166. }
  167. return eq;
  168. }
  169. bool operator!=(const Stack<T> &s1) {
  170. return !(*this == s1);
  171. }
  172. void push(T n) {
  173. if (full() == true) grow();
  174. *(--_top) = n;
  175. }
  176. T pop() {
  177. T x = *_top;
  178. if (empty() == false) _top++;
  179. &n
  180.  
  181.  
  182. #endif
Reputation Points: 8
Solved Threads: 0
Posting Pro in Training
NinjaLink is offline Offline
416 posts
since Mar 2008
Oct 26th, 2009
0
Re: C++ Compare stacks help!
Did you even bother to compile this before posting it? Even a cursory look at it makes it obvious that it won't compile. Look at line 179 for instance. There is no "actual output" because this doesn't even come close to compiling or running. Proofread before you post. There's an "edit" button if you accidentally copy and paste the wrong thing or whatever. I can't imagine what else this post could have been.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Oct 26th, 2009
0
Re: C++ Compare stacks help!
I apologize. I corrected the information below. I do not know how that other stuff got in that post. Right now, everything compiles and it looks fine except that I do not know how to print out the information/numbers that are contained in stacks: s1 and s2.



Current output:

C++ Syntax (Toggle Plain Text)
  1. s1:
  2. s2:
  3. s1 is not equal to s2
  4. s2:
  5. s1 is equal s2
  6. s1:
  7. s1 is not equal to s2


Expected output:

C++ Syntax (Toggle Plain Text)
  1. s1: 20 10
  2. s2: 10
  3. s1 is not equal to s2
  4. s2: 20 10
  5. s1 is equal s2
  6. s1: 20 30
  7. s1 is not equal to s2




main.cpp


C++ Syntax (Toggle Plain Text)
  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. if (s1 == s2)
  14. {
  15. cout << "empty stacks are equal" << endl;
  16. }
  17. s1.push(10); s1.push(20);
  18. s2.push(10);
  19. cout << "s1: "<<endl;
  20. cout << "s2: "<<endl;
  21.  
  22. if (s1 != s2)
  23. cout << "s1 is not equal to s2" << endl;
  24. s2.push(20);
  25. cout << "s2: "<<endl;
  26.  
  27.  
  28. if (s1 == s2)
  29. {
  30. cout << "s1 is equal to ";
  31. }
  32.  
  33. else
  34. {
  35. cout << "s1 is not equal to " << endl;
  36. }
  37. cout << "s2" << endl;
  38. s1.pop(); s1.pop();
  39. s1.push(30); s1.push(20);
  40. cout << "s1: "<<endl;
  41.  
  42. if (s1 != s2)
  43. cout << "s1 is not equal to s2" << endl;
  44.  
  45. system("PAUSE");
  46. return 0;
  47. }




header file:


C++ Syntax (Toggle Plain Text)
  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.  
  31.  
  32. private:
  33. int maxStackSize;
  34. int stackTop;
  35. Type *list;
  36. void copyStack(const stackType<Type>& otherStack);
  37. bool isEqual(const stackType<Type>&);
  38.  
  39. };
  40.  
  41. template<class Type>
  42. void stackType<Type>::copyStack(const stackType<Type>& otherStack)
  43. {
  44. delete [] list;
  45. maxStackSize = otherStack.maxStackSize;
  46. stackTop = otherStack.stackTop;
  47.  
  48. list = new Type[maxStackSize];
  49. assert(list != NULL);
  50.  
  51. for(int j = 0; j < stackTop; j++)
  52. list[j] = otherStack.list[j];
  53.  
  54. }
  55.  
  56. template<class Type>
  57. stackType<Type>::stackType(const stackType<Type>& otherStack)
  58. {
  59. list = NULL;
  60.  
  61. copyStack(otherStack);
  62.  
  63. }
  64.  
  65. template<class Type>
  66. const stackType<Type>& stackType<Type>::operator=(const stackType<Type>& otherStack)
  67. {
  68. if (this != &otherStack)
  69. copyStack(otherStack);
  70.  
  71. return *this;
  72. }
  73.  
  74. template<class Type>
  75. void stackType<Type>::initializeStack()
  76. {
  77. stackTop = 0;
  78. }
  79.  
  80. template<class Type>
  81. void stackType<Type>::destroyStack()
  82. {
  83. stackTop = 0;
  84. }
  85.  
  86. template<class Type>
  87. bool stackType<Type>::isEmptyStack()
  88. {
  89. return(stackTop == maxStackSize);
  90. }
  91.  
  92. template<class Type>
  93. bool stackType<Type>::isFullStack()
  94. {
  95. return(stackTop == maxStackSize);
  96. }
  97.  
  98. template<class Type>
  99. void stackType<Type>::push(const Type& newItem)
  100. {
  101. if(!isFullStack())
  102. {
  103. list[stackTop] = newItem;
  104.  
  105. stackTop++;
  106. }
  107.  
  108. else
  109. cerr<<"Cannot add to a full stack."<<endl;
  110. }
  111.  
  112. template<class Type>
  113. Type stackType<Type>::top()
  114. {
  115. assert(stackTop != 0);
  116.  
  117. return list[stackTop - 1];
  118. }
  119.  
  120. template<class Type>
  121. void stackType<Type>::pop()
  122. {
  123. if(!isEmptyStack())
  124. {
  125. stackTop--;
  126. }
  127. else
  128. cerr<<"Cannot remove from an empty stack."<<endl;
  129. }
  130.  
  131. template<class Type>
  132. stackType<Type>::stackType(int stackSize)
  133. {
  134. if(stackSize <= 0)
  135. {
  136. cerr<<"The size of the array to hold the stack must "
  137. <<"be positive."<<endl;
  138. cerr<<"Creating an array of size 100."<<endl;
  139.  
  140. maxStackSize = 100;
  141. }
  142.  
  143. else
  144. maxStackSize = stackSize;
  145.  
  146. stackTop = 0;
  147.  
  148. list = new Type[maxStackSize];
  149. }
  150.  
  151. template<class Type>
  152. stackType<Type>::~stackType()
  153. {
  154. delete [] list;
  155. }
  156.  
  157.  
  158.  
  159. template<class Type>
  160. bool stackType<Type>::isEqual(const stackType<Type>& otherStack) {
  161. bool bRet = false;
  162. if (otherStack.maxStackSize == maxStackSize && otherStack.stackTop == stackTop){
  163. bRet = true;
  164. for (int j = 0; j < stackTop; ++j) {
  165. if (otherStack.list[j] != list[j]) {
  166. // cout << "!=( " << j << ") " << otherStack.list[j] << "!=" << list[j];
  167. bRet = false;
  168. break;
  169. }
  170. }
  171. }
  172. return bRet;
  173. }
  174.  
  175. template<class Type>
  176. bool stackType<Type>::operator==(const stackType<Type>& otherStack) {
  177. return isEqual(otherStack);
  178. }
  179.  
  180. template<class Type>
  181. bool stackType<Type>::operator!=(const stackType<Type>& otherStack) {
  182. return !isEqual(otherStack); //!(*this == otherStack);
  183. }
  184.  
  185.  
  186. #endif
Last edited by NinjaLink; Oct 26th, 2009 at 11:46 am.
Reputation Points: 8
Solved Threads: 0
Posting Pro in Training
NinjaLink is offline Offline
416 posts
since Mar 2008
Oct 26th, 2009
0
Re: C++ Compare stacks help!
Write a << operator. In line 52, you use the [] operator, so you have the ability to isolate the individual elements of list. Loop through the elements in list, isolate an element, then display that individual list element using its own << operator (one that has nothing to do with stackType's << operator. int, string, double, etc., all have their own << operators already).

In the future, write the << operator early, long before the == and != operators. How does one debug if he/she cannot display an object's contents?
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Reading characters into array from file
Next Thread in C++ Forum Timeline: access violation on form close





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC