Error : Copying user defined stack object to Another

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>::pStack()
{
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>::eek: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>::pStack(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>::pop()
{
if(IsEmpty())
cout<<" Stack is empty "<<endl;

ListNode * ptr = topOfStack ;
topOfStack = topOfStack->next;
delete ptr;
}

template<typename Object>
void PStack<Object>::push(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>::Display()
{
ListNode * ptr = topOfStack ;

while(ptr != NULL)
{
cout<< ptr->element << endl ;
ptr = ptr->next;
}

}


////////////////////////////////////////////////////////////////////////////////////////////
 
B

Bo Persson

Pallav said:
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;
}

large snip!

template<typename Object>
bool PStack<Object>::IsEmpty()
{
return (topOfStack == NULL);
}


template<typename Object>
PStack<Object> & PStack<Object>::eek:perator = (const PStack<Object> &
rhs)
{
if(this != &rhs )
{
MakeEmpty();

if( rhs.IsEmpty() )
return *this;

Strange, I get the error right here!

Let's see, we call IsEmpty() on a const reference. Is the IsEmpty()
function declared const? No?


Bo Persson
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,968
Messages
2,570,149
Members
46,695
Latest member
StanleyDri

Latest Threads

Top