| | |
C++ Compare stacks help!
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2008
Posts: 365
Reputation:
Solved Threads: 0
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
main.cpp
header file
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
C++ Syntax (Toggle Plain Text)
empty stacks are equal s1: 20 10 s2: 10 s1 is not equal to s2 s2: 20 10 s1 is equal to 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: "; cout << "s2: "; cout << "s1 "; if (s1 != s2) cout << "!"; cout << "= s2" << endl; s2.push(20); cout << "s2: "; 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: "; 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(); 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; } #endif
Last edited by NinjaLink; 33 Days Ago at 10:11 pm.
•
•
Join Date: Jan 2008
Posts: 3,813
Reputation:
Solved Threads: 501
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.
0
#3 33 Days Ago
Pop elements from both stacks and compare whether they are equal.
•
•
Join Date: Mar 2008
Posts: 365
Reputation:
Solved Threads: 0
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.
error:155 bool operator==(const stackType<Type>&)' must take exactly two arguments
But I dont really know what other arguement to take.
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>& otherStack); bool operator!=(const stackType<Type>& otherStack); 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; } template<class Type> bool operator== (const stackType<Type>& otherStack) { bool eq=true; } template<class Type> bool operator!= (const stackType<Type>& otherStack) { return !( this == otherStack ); } #endif
Last edited by NinjaLink; 33 Days Ago at 12:35 pm.
•
•
Join Date: Jan 2008
Posts: 3,813
Reputation:
Solved Threads: 501
0
#7 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=?
const stackType<Type>& stackType<Type>::operator=(const stackType<Type>& otherStack)
Now look at your == operator:
C++ Syntax (Toggle Plain Text)
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?
•
•
Join Date: Mar 2008
Posts: 365
Reputation:
Solved Threads: 0
0
#8 33 Days Ago
The reason I asked was because when I made the change:
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:
C++ Syntax (Toggle Plain Text)
bool stackType<Type>& operator==(bool stackType<Type>&); bool stackType<Type>& operator!= (bool stackType<Type>&); template<class Type> bool stackType<Type>::operator==(bool stackType<Type>& otherStack) { bool eq=true; } template<class Type> bool stackType<Type>::operator!=(bool stackType<Type>& otherStack) { return !(this == &otherStack); }
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:
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 stackType<Type>& operator==(bool stackType<Type>&); bool stackType<Type>& operator!= (bool stackType<Type>&); 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; } template<class Type> bool stackType<Type>::operator==(bool stackType<Type>& otherStack) { bool eq=true; } template<class Type> bool stackType<Type>::operator!=(bool stackType<Type>& otherStack) { return !(this == &otherStack); } #endif
•
•
Join Date: Jan 2008
Posts: 3,813
Reputation:
Solved Threads: 501
0
#9 33 Days Ago
Go back to post 4. Change lines 154 - 166 to this:
All you want to do is add
[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]
C++ Syntax (Toggle Plain Text)
template<class Type> bool stackType<Type>::operator== (const stackType<Type>& otherStack) { bool eq=true; } template<class Type> bool stackType<Type>::operator!= (const stackType<Type>& otherStack) { return !( this == otherStack ); }
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.
•
•
Join Date: Mar 2008
Posts: 365
Reputation:
Solved Threads: 0
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:
main.cpp
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:
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); }; 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>::operator== (const stackType<Type>& otherStack) { if (otherStack.isFullStack() != isFullStack()) cerr<<"They are not the same size"<<endl; for(int j = 0 ; j < isFullStack(); ++j) { if (otherStack.list[j] != list[j]) return false; else return true; } } template<class Type> bool stackType<Type>::operator!= (const stackType<Type>& otherStack) { return !(*this == otherStack); } #endif
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: "; cout << "s2: "; cout << "s1 "; if (s1 != s2) cout << "!"; cout << "= s2" << endl; s2.push(20); cout << "s2: "; 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: "; cout << "s1 "; if (s1 != s2) cout << " is"; cout << " not equal to s2" << endl; system ("pause"); return 0; }
![]() |
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 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 visual visualstudio win32 windows winsock wordfrequency wxwidgets






