| | |
C++ Compare stacks help!
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jan 2008
Posts: 3,813
Reputation:
Solved Threads: 501
0
#11 32 Days Ago
You need to read these error messages carefully. They tell you exactly what's wrong.
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.
158 passing `const stackType<int>' as `this' argument of `bool stackType<Type>::isFullStack() [with Type = int]' discards qualifiersThe 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; 32 Days Ago at 3:57 pm.
•
•
Join Date: Mar 2008
Posts: 365
Reputation:
Solved Threads: 0
-1
#14 32 Days Ago
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
header file:
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)
main.cpp #include <iostream> #include <stdlib.h> #include "stack.h" using namespace std; int main() { stackType<int> s1; stackType<int> s2; if (s1 == s2) { cout << "empty stacks are equal" << endl; } s1.push(10); s1.push(20); s2.push(10); cout << "s1: "<<endl; cout << "s2: "<<endl; cout << "s1 "; if (s1 != s2) cout << "!"; cout << "= s2" << endl; s2.push(20); cout << "s2: "<<endl; cout << "s1 "; if (s1 == s2) { cout << "is equal "; } else { cout << "is not equal " << endl; } cout << "s2" << endl; s1.pop(); s1.pop(); s1.push(30); s1.push(20); cout << "s1: "<<endl; cout << "s1 "; if (s1 != s2) cout << "is"; cout << " not equal to s2" << endl; system ("pause"); return 0; }
header file:
C++ Syntax (Toggle Plain Text)
#ifndef H_StackType #define H_StackType #include <iostream> #include <cassert> using namespace std; template<class Type> class stackType { public: const stackType<Type>& operator=(const stackType<Type>&); void initializeStack(); bool isEmptyStack(); bool isFullStack(); bool operator==(const Stack<T> &s1) bool operator!=(const Stack<T> &s1) void destroyStack(); void push(const Type& newItem); Type top(); void pop(); stackType(int stackSize = 100); stackType(const stackType<Type>& otherStack); ~stackType(); private: int maxStackSize; int stackTop; Type *list; void copyStack(const stackType<Type>& otherStack); }; template<class Type> void stackType<Type>::copyStack(const stackType<Type>& otherStack) { delete [] list; maxStackSize = otherStack.maxStackSize; stackTop = otherStack.stackTop; list = new Type[maxStackSize]; assert(list != NULL); for(int j = 0; j < stackTop; j++) list[j] = otherStack.list[j]; } template<class Type> stackType<Type>::stackType(const stackType<Type>& otherStack) { list = NULL; copyStack(otherStack); } template<class Type> const stackType<Type>& stackType<Type>::operator=(const stackType<Type>& otherStack) { if (this != &otherStack) copyStack(otherStack); return *this; } template<class Type> void stackType<Type>::initializeStack() { stackTop = 0; } template<class Type> void stackType<Type>::destroyStack() { stackTop = 0; } template<class Type> bool stackType<Type>::isEmptyStack() { return(stackTop == maxStackSize); } template<class Type> bool stackType<Type>::isFullStack() { return(stackTop == maxStackSize); } template<class Type> void stackType<Type>::push(const Type& newItem) { if(!isFullStack()) { list[stackTop] = newItem; stackTop++; } else cerr<<"Cannot add to a full stack."<<endl; } template<class Type> Type stackType<Type>::top() { assert(stackTop != 0); return list[stackTop - 1]; } template<class Type> void stackType<Type>::pop() { if(!isEmptyStack()) { stackTop--; } else cerr<<"Cannot remove from an empty stack."<<endl; } template<class Type> stackType<Type>::stackType(int stackSize) { if(stackSize <= 0) { cerr<<"The size of the array to hold the stack must " <<"be positive."<<endl; cerr<<"Creating an array of size 100."<<endl; maxStackSize = 100; } else maxStackSize = stackSize; stackTop = 0; list = new Type[maxStackSize]; } template<class Type> stackType<Type>::~stackType() { delete [] list; } bool operator==(const Stack<T> &s1) { bool eq = true; if (s1.stackLen() != this->stackLen()) { eq = false; } else if (s1.empty() == false) { size_t len = this->stackLen(); T *p = this->_top; T *q = s1._top; for (size_t i = 0; (i < len) && (eq == true); i++) { eq = (*p++ == *q++); } } return eq; } bool operator!=(const Stack<T> &s1) { return !(*this == s1); } void push(T n) { if (full() == true) grow(); *(--_top) = n; } T pop() { T x = *_top; if (empty() == false) _top++; &n #endif
•
•
Join Date: Jan 2008
Posts: 3,813
Reputation:
Solved Threads: 501
0
#15 31 Days Ago
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.
•
•
Join Date: Mar 2008
Posts: 365
Reputation:
Solved Threads: 0
0
#16 31 Days Ago
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:
Expected output:
main.cpp
header file:
Current output:
C++ Syntax (Toggle Plain Text)
s1: s2: s1 is not equal to s2 s2: s1 is equal s2 s1: s1 is not equal to s2
Expected output:
C++ Syntax (Toggle Plain Text)
s1: 20 10 s2: 10 s1 is not equal to s2 s2: 20 10 s1 is equal s2 s1: 20 30 s1 is not equal to s2
main.cpp
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <stdlib.h> #include "stack.h" using namespace std; int main() { stackType<int> s1; stackType<int> s2; if (s1 == s2) { cout << "empty stacks are equal" << endl; } s1.push(10); s1.push(20); s2.push(10); cout << "s1: "<<endl; cout << "s2: "<<endl; if (s1 != s2) cout << "s1 is not equal to s2" << endl; s2.push(20); cout << "s2: "<<endl; if (s1 == s2) { cout << "s1 is equal to "; } else { cout << "s1 is not equal to " << endl; } cout << "s2" << endl; s1.pop(); s1.pop(); s1.push(30); s1.push(20); cout << "s1: "<<endl; if (s1 != s2) cout << "s1 is not equal to s2" << endl; system("PAUSE"); return 0; }
header file:
C++ Syntax (Toggle Plain Text)
#ifndef H_StackType #define H_StackType #include <iostream> #include <cassert> using namespace std; template<class Type> class stackType { public: const stackType<Type>& operator=(const stackType<Type>&); void initializeStack(); bool isEmptyStack(); bool isFullStack(); void destroyStack(); void push(const Type& newItem); Type top(); void pop(); stackType(int stackSize = 100); stackType(const stackType<Type>& otherStack); ~stackType(); bool operator== (const stackType<Type>&); bool operator!= (const stackType<Type>&); private: int maxStackSize; int stackTop; Type *list; void copyStack(const stackType<Type>& otherStack); bool isEqual(const stackType<Type>&); }; template<class Type> void stackType<Type>::copyStack(const stackType<Type>& otherStack) { delete [] list; maxStackSize = otherStack.maxStackSize; stackTop = otherStack.stackTop; list = new Type[maxStackSize]; assert(list != NULL); for(int j = 0; j < stackTop; j++) list[j] = otherStack.list[j]; } template<class Type> stackType<Type>::stackType(const stackType<Type>& otherStack) { list = NULL; copyStack(otherStack); } template<class Type> const stackType<Type>& stackType<Type>::operator=(const stackType<Type>& otherStack) { if (this != &otherStack) copyStack(otherStack); return *this; } template<class Type> void stackType<Type>::initializeStack() { stackTop = 0; } template<class Type> void stackType<Type>::destroyStack() { stackTop = 0; } template<class Type> bool stackType<Type>::isEmptyStack() { return(stackTop == maxStackSize); } template<class Type> bool stackType<Type>::isFullStack() { return(stackTop == maxStackSize); } template<class Type> void stackType<Type>::push(const Type& newItem) { if(!isFullStack()) { list[stackTop] = newItem; stackTop++; } else cerr<<"Cannot add to a full stack."<<endl; } template<class Type> Type stackType<Type>::top() { assert(stackTop != 0); return list[stackTop - 1]; } template<class Type> void stackType<Type>::pop() { if(!isEmptyStack()) { stackTop--; } else cerr<<"Cannot remove from an empty stack."<<endl; } template<class Type> stackType<Type>::stackType(int stackSize) { if(stackSize <= 0) { cerr<<"The size of the array to hold the stack must " <<"be positive."<<endl; cerr<<"Creating an array of size 100."<<endl; maxStackSize = 100; } else maxStackSize = stackSize; stackTop = 0; list = new Type[maxStackSize]; } template<class Type> stackType<Type>::~stackType() { delete [] list; } template<class Type> bool stackType<Type>::isEqual(const stackType<Type>& otherStack) { bool bRet = false; if (otherStack.maxStackSize == maxStackSize && otherStack.stackTop == stackTop){ bRet = true; for (int j = 0; j < stackTop; ++j) { if (otherStack.list[j] != list[j]) { // cout << "!=( " << j << ") " << otherStack.list[j] << "!=" << list[j]; bRet = false; break; } } } return bRet; } template<class Type> bool stackType<Type>::operator==(const stackType<Type>& otherStack) { return isEqual(otherStack); } template<class Type> bool stackType<Type>::operator!=(const stackType<Type>& otherStack) { return !isEqual(otherStack); //!(*this == otherStack); } #endif
Last edited by NinjaLink; 31 Days Ago at 11:46 am.
•
•
Join Date: Jan 2008
Posts: 3,813
Reputation:
Solved Threads: 501
0
#17 31 Days Ago
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?
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?
![]() |
Similar Threads
- Loan compare issue (Java)
- simple palindrome program (C++)
- how to compare operators if they are characters. (C++)
- Quick question on Stacks, queue and heap (C++)
- Stacks - balanced parentheses (C++)
- compare (C++)
- String search in a file compare? (Computer Science)
- Compare strings... (C++)
Other Threads in the C++ Forum
- Previous Thread: Reading characters into array from file
- Next Thread: access violation on form close
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






