So my homework assignment is to overload the == operator to see if 2 stacks are the same or not. I overloaded the operator and wrote the definition, but I have hit a roadblock as to why it's not functioning correctly.
The copyStack function written by the author of our text book is:
1 2 3 4 5 6 7 8 9 10 11 12 13
template <class Type>
void stackType<Type>::copyStack(const stackType<Type>& otherStack)
{
delete [] list;
maxStackSize = otherStack.maxStackSize;
stackTop = otherStack.stackTop;
list = new Type[maxStackSize];
//copy otherStack into this stack
for (int j = 0; j < stackTop; j++)
list[j] = otherStack.list[j];
} //end copyStack
template <class Type>
Type stackType<Type>::top() const
{
assert(stackTop != 0); //if stack is empty,
//terminate the program
return list[stackTop - 1]; //return the element of the
//stack indicated by
//stackTop - 1
}//end top
And finally the pop function:
1 2 3 4 5 6 7 8
template <class Type>
void stackType<Type>::pop()
{
if (!isEmptyStack())
stackTop--; //decrement stackTop
else
cout << "Cannot remove from an empty stack." << endl;
}//end pop
I am using the copyStack function in my operator overload function to copy the stacks that are being compared since popping the stacks to compare destroys them.
The 2 problems I am having is that the function constantly returns false even when the stacks are the same, and after comparing them, the original stacks that are copied are destroyed. Can anybody help me with this and point out where I am messing up? Thank you!
That definitely makes more sense now that I take a second look at it. Thank you for pointing out the proper use of that function. However, now I get a compilation error in DevC++.
C:\CPP\8 - Copy (2) - Copy\myStack.h In member function `bool stackType<Type>::operator==(stackType<Type>&) [with Type = int]':
73 C:\CPP\8 - Copy (2) - Copy\testProgStack.cpp instantiated from here
210 C:\CPP\8 - Copy (2) - Copy\myStack.h invalid conversion from `stackType<int>* const' to `int'
210 C:\CPP\8 - Copy (2) - Copy\myStack.h initializing argument 1 of `stackType<Type>::stackType(int) [with Type = int]'
C:\CPP\8 - Copy (2) - Copy\Makefile.win [Build Error] [testProgStack.o] Error 1
I don't know how to handle cryptic error messages like this especially when I don't know what exactly is causing it. It seems like I should be able to convert from the stacktype<int> to int no problem since they both are int variables. I tried making the operator overloading a non-member/friend function, but ran into more errors than I could handle. Any assistance is greatly appreciated! Thank you!
Oh, wow. These error messages suck. It took me a while to understand what they meant, even though I knew what the problem was with that line. /*210*/ stackA.copyStack(*this); //note the asterisk
Get yourself a newer compiler now. Dev-C++ comes, IINM, with a MinGW 3.x. MinGW is now on 4.5.
Thank you! It now compiles. I did have an issue getting the function to return the proper status, but I figured that out. I plan on getting Visual Studio 2010 provided by my school, but when I experimented with it before the interface was intimidating and it took forever to compile or spit out an error message, so I went back to what I knew.
Visual Studio is actually pretty easy to use even for beginners - as long as you are able to ignore everything you don't need immediately. Everyone I know would recommend against using Dev-C++, because it's simply very old, buggy, and lacks many features modern IDE's provide.
I would say to use Code::Blocks, it a lot more standard C and C++ than Visual Studio, but I use both. Code::Blocks will defiantly tell you a lot more than Dev-C++ will.