So I'm about ready to toss my computer out of a window, and I'm now at the point where I figure I should ask for help before I destroy an expensive piece of equipment.
My program has a list that is filled in automatically (I manually input the items), and it just outputs it. Then, I have another block of code that is to output it again using assignment operators, and then a third copy with a copy constructor.
The assignment operator crashes the program, but if I comment it out to let it get to the copy constructor, the list comes out empty.
Anything that says "TODO" you can ignore, that's noted for me to fix later.
Here is where all the action happens:
List.cpp
(This is not my main function, there's nothing wrong there)
List::List() : m_pFront(0), m_pBack(0), _pData(0)
{}
List::~List()
{
Clear();
}
List::List(const char* p)
{
if(!p)
{
_pData = 0;
return;
}
if(strlen(p) == 0)
{
_pData = 0;
return;
}
_pData = new char[strlen(p) + 1];
strcpy(_pData, p);
}
void List::Clear()
{
//delete
if(!m_pFront)
{
return;
}
delete m_pFront;
Node* p = m_pBack;
//Walking the list
while(p)
{
//Get a pointer to the next in the list
Node* pTemp = p -> m_pNext;
delete p;
//Move p along the list
p = pTemp;
}
m_pFront = 0;
m_pBack = 0;
}
void List::PushFront(std::string data)
{
//create a new node
Node* p = new Node(data);
//Empty list
if(!m_pFront)
{
m_pFront = p;
m_pBack = p;
}
else //Not empty list
{
p -> m_pNext = m_pFront;
m_pFront -> m_pPrev = p;
m_pFront = p;
}
}
void List::PushBack(std::string data)
{
Node* p = new Node(data);
if(!m_pBack)
{
m_pFront = p;
m_pBack = p;
}
else
{
p -> m_pPrev = m_pBack;
m_pBack -> m_pNext = p;
m_pBack = p;
}
}
void List::PopFront()
{
if(m_pFront == 0)
{
//TODO: we need to handle this problem
return;
}
if(m_pBack == m_pFront)
{
Clear();
return;
}
Node* p = m_pFront;
m_pFront = m_pFront -> m_pNext;
p -> m_pNext = 0;
m_pFront -> m_pPrev = 0;
delete p;
}
void List::PopBack()
{
if(m_pBack == 0)
{
//TODO: we need to handle this problem
return;
}
if(m_pBack == m_pFront)
{
Clear();
return;
}
Node* p = m_pBack;
m_pBack = m_pBack -> m_pPrev;
p -> m_pPrev = 0;
m_pBack -> m_pNext = 0;
delete p;
}
ostream& List::OutPut(ostream& os)
{
if(Empty() == true)
{
os << "<empty>";
}
else
{
m_pFront -> OutputNode(os);
}
return os;
}
std::string& List::Back() const
{
if(m_pBack == 0)
{
//TODO: we need to handle this problem
}
return m_pBack -> GetData();
}
std::string& List::Front() const
{
if(m_pFront == 0)
{
//TODO: we need to handle this problem
}
return m_pFront -> GetData();
}
//Copy Constructor
List::List(const List& str)
{
if(Empty() == true)
{
_pData = 0;
return;
}
_pData = new char[strlen(str._pData) + 1];
strcpy(_pData, str._pData);
}
//Deep copy
List& List::operator=(const List& str)
{
if(&str == this)
{
return *this;
}
delete [] _pData;
_pData = new char[strlen(str._pData) + 1];
strcpy(_pData, str._pData);
return *this;
}
EDIT: This is where the List class is made, if this needs to be seen as well
List.h
class List
{
public:
List();
List(const char* p);
//Copy constructor
List(const List& str);
//Deep Copy
List& operator=(const List& str);
~List();
void Clear();
//Adds to the front
void PushFront(std::string data);
//adds to the back
void PushBack(std::string data);
//removes from the front
void PopFront();
//removes from the back
void PopBack();
//gets the back value
std::string& Back() const;
//gets the from value
std::string& Front() const;
bool Empty() const {return m_pFront == 0;}
ostream& OutPut(ostream& os);
private:
Node* m_pFront;
Node* m_pBack;
char* _pData;
};