Hi
I'm having a big problem with stacks now whilst preparing for the exam. I'm trying to do question 01 on page 466 of Malik.
I'm using the source code provided for chapter 7 the UNISA 2007 CD.
My problem is with the copyStack and copy constructor: below is my code for the copy constructor:
template<class Type> //copy constructor
linkedStackType<Type>::linkedStackType(const linkedStackType<Type>& otherStack)
{
initializeStack();
copyStack(otherStack);
}//end copy constructor
The initializeStack(); statement causes the program to crash. if i changed it to stackTop = NULL; then this constructor runs fine, until i try to use otherStack thats been passed to copyStack. Then the compiler starts b1tchin' about
instantiated from `linkedStackType<Type>::linkedStackType(const linkedStackType<Type>& ) [with Type = int]' and some error about
this
here is what i did in the copyStack method:
template<class Type>
void linkedStackType<Type>::copyStack(const linkedStackType<Type>& otherStack)
{
nodeType<Type> *current; //to traverse the stack
linkedStackType<Type> tempStack; //temp list to store reverse read stack values
current = otherStack.top();
while(current !=NULL) //while otherstack has more elements, fill up tempStack
{
tempStack.push(current->info);
current = current->link;
}
current = tempStack.top(); //now going to copy values from tempStack to the
//new stack in the correct order.
while(current !=NULL) //while my tempstack has values, fill up the stack
{ //(not otherstack)
push(current->info);
current = current->link;
}
}
and here is my test program:
#include <iostream>
#include "linkedStack.h"
using namespace std;
int main()
{
linkedStackType<int> list1;
for(int i = 1; i < 50; i++)
list1.push(i);
list1.print();
linkedStackType<int> list2(list1);
list2.print();
return 0;
}
here is the print() incase you are wondering:
template<class Type>
void linkedStackType<Type>::print()
{
nodeType<Type> *current;
current = stackTop;
while(current != NULL)
{
cout<<current->info<<" ";
current = current->link;
}
cout << endl;
}
this works 100%
I also noted that in the copy constructor and the copyStack methods if i type
otherStack(dot) that no code completion box comes up which leads me to believe that the otherStack is not being seen somewhere.
Any help would be greatly appreciated.
Thanx