P
Pallav singh
Hi
i am getting error while copying one Stack Object to Another
Thanks
Pallav Singh
///////////////////////////////////////////////////////////////////////////////////
#include "PStack.cpp"
int main()
{
PStack<int> obj1 , obj2;
obj1.Push(1);
obj1.Push(2);
obj1.Push(3);
obj1.Push(4);
obj1.Push(5);
obj1.Display();
obj2 = obj1; // getting Error Here
obj2.Display();
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////////
File : PStack.h
#include <iostream>
using namespace std;
template<typename Object>
class PStack
{
private :
struct ListNode
{
ListNode * next ;
Object element;
ListNode(const Object & data , ListNode * link= NULL):element
(data),next(link)
{}
};
ListNode * topOfStack;
int count;
public :
PStack();
~PStack();
PStack & operator = (const PStack & rhs);
PStack(const PStack & rhs);
void Pop();
void Push(const Object & x);
const Object & Top();
int Count();
bool IsEmpty();
void MakeEmpty();
void Display();
};
////////////////////////////////////////////////////////////////////////////////////////////
File :: PStack.cpp
#include "PStack.h"
template<typename Object>
PStack<Object>:Stack()
{
topOfStack = NULL ;
}
template<typename Object>
PStack<Object>::~PStack()
{
MakeEmpty();
}
template<typename Object>
void PStack<Object>::MakeEmpty()
{
while( !IsEmpty())
{ Pop(); }
}
template<typename Object>
bool PStack<Object>::IsEmpty()
{
return (topOfStack == NULL);
}
template<typename Object>
PStack<Object> & PStack<Object>:perator = (const PStack<Object> &
rhs)
{
if(this != &rhs )
{
MakeEmpty();
if( rhs.IsEmpty() )
return *this;
ListNode * rptr = rhs.topOfStack;
ListNode * ptr = new ListNode(rptr->element);
topOfStack = ptr;
for( rptr = rptr->next ; rptr != NULL ; rptr = rptr->next)
{
ptr->next = new ListNode(rptr->element);
ptr = ptr->next;
}
}
return *this;
}
template<typename Object>
PStack<Object>:Stack(const PStack<Object> & rhs)
{
if(rhs.IsEmpty())
topOfStack = rhs.topOfStack;
else
{
ListNode * rptr = rhs.topOfStack;
ListNode * ptr = new ListNode(rptr->element);
topOfStack = ptr;
for( rptr = rptr->next ; rptr != NULL ; rptr = rptr->next)
{
ptr->next = new ListNode(rptr->element);
ptr = ptr->next;
}
}
}
template<typename Object>
void PStack<Object>:op()
{
if(IsEmpty())
cout<<" Stack is empty "<<endl;
ListNode * ptr = topOfStack ;
topOfStack = topOfStack->next;
delete ptr;
}
template<typename Object>
void PStack<Object>:ush(const Object & x)
{
topOfStack = new ListNode(x,topOfStack);
}
template<typename Object>
const Object & PStack<Object>::Top()
{
return topOfStack->element;
}
template<typename Object>
int PStack<Object>::Count()
{
return count;
}
template<typename Object>
void PStack<Object>:isplay()
{
ListNode * ptr = topOfStack ;
while(ptr != NULL)
{
cout<< ptr->element << endl ;
ptr = ptr->next;
}
}
////////////////////////////////////////////////////////////////////////////////////////////
i am getting error while copying one Stack Object to Another
Thanks
Pallav Singh
///////////////////////////////////////////////////////////////////////////////////
#include "PStack.cpp"
int main()
{
PStack<int> obj1 , obj2;
obj1.Push(1);
obj1.Push(2);
obj1.Push(3);
obj1.Push(4);
obj1.Push(5);
obj1.Display();
obj2 = obj1; // getting Error Here
obj2.Display();
return 0;
}
////////////////////////////////////////////////////////////////////////////////////////////
File : PStack.h
#include <iostream>
using namespace std;
template<typename Object>
class PStack
{
private :
struct ListNode
{
ListNode * next ;
Object element;
ListNode(const Object & data , ListNode * link= NULL):element
(data),next(link)
{}
};
ListNode * topOfStack;
int count;
public :
PStack();
~PStack();
PStack & operator = (const PStack & rhs);
PStack(const PStack & rhs);
void Pop();
void Push(const Object & x);
const Object & Top();
int Count();
bool IsEmpty();
void MakeEmpty();
void Display();
};
////////////////////////////////////////////////////////////////////////////////////////////
File :: PStack.cpp
#include "PStack.h"
template<typename Object>
PStack<Object>:Stack()
{
topOfStack = NULL ;
}
template<typename Object>
PStack<Object>::~PStack()
{
MakeEmpty();
}
template<typename Object>
void PStack<Object>::MakeEmpty()
{
while( !IsEmpty())
{ Pop(); }
}
template<typename Object>
bool PStack<Object>::IsEmpty()
{
return (topOfStack == NULL);
}
template<typename Object>
PStack<Object> & PStack<Object>:perator = (const PStack<Object> &
rhs)
{
if(this != &rhs )
{
MakeEmpty();
if( rhs.IsEmpty() )
return *this;
ListNode * rptr = rhs.topOfStack;
ListNode * ptr = new ListNode(rptr->element);
topOfStack = ptr;
for( rptr = rptr->next ; rptr != NULL ; rptr = rptr->next)
{
ptr->next = new ListNode(rptr->element);
ptr = ptr->next;
}
}
return *this;
}
template<typename Object>
PStack<Object>:Stack(const PStack<Object> & rhs)
{
if(rhs.IsEmpty())
topOfStack = rhs.topOfStack;
else
{
ListNode * rptr = rhs.topOfStack;
ListNode * ptr = new ListNode(rptr->element);
topOfStack = ptr;
for( rptr = rptr->next ; rptr != NULL ; rptr = rptr->next)
{
ptr->next = new ListNode(rptr->element);
ptr = ptr->next;
}
}
}
template<typename Object>
void PStack<Object>:op()
{
if(IsEmpty())
cout<<" Stack is empty "<<endl;
ListNode * ptr = topOfStack ;
topOfStack = topOfStack->next;
delete ptr;
}
template<typename Object>
void PStack<Object>:ush(const Object & x)
{
topOfStack = new ListNode(x,topOfStack);
}
template<typename Object>
const Object & PStack<Object>::Top()
{
return topOfStack->element;
}
template<typename Object>
int PStack<Object>::Count()
{
return count;
}
template<typename Object>
void PStack<Object>:isplay()
{
ListNode * ptr = topOfStack ;
while(ptr != NULL)
{
cout<< ptr->element << endl ;
ptr = ptr->next;
}
}
////////////////////////////////////////////////////////////////////////////////////////////