0

我有一个手动放入列表的程序,它只是一堆像这样扔进去的名字:list.PushBack("Frank");list.PushFront("George");

我当前的问题是,当我尝试通过复制构造函数和赋值运算符运行已经创建的列表时,列表出现“”(在 isEmpty 函数中定义)

忽略所有“TODO”评论点,这些都是我自己的心理笔记。

还有一些我用于调试目的的随机 cout 语句,并希望保留在那里直到一切正常。

以下是相关代码:

列表.h

class Node;

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* m_pData;

};

列表.cpp

List::List() : m_pFront(0), m_pBack(0), m_pData(0)
{}

List::~List()
{
    Clear();
}

void List::Clear()
{
    //delete 
    if(!m_pFront)
    {
        return;
    }
    delete m_pFront;
    delete m_pData;

    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(str.m_pData)
{
    m_pData = new char[strlen(str.m_pData) + 1];
    strcpy(m_pData, str.m_pData);
}
else
{
    m_pData = 0;
}
}

//Deep copy
List& List::operator=(const List& str)
{   
//Check for self assignment
if(this == &str)
    {
        return *this;
    }
//Deallocate any value the string is holding
delete [] m_pData;
//Now we need to deep copy m_pData
if(str.m_pData)
{
    //Allocate memory for the copy
    m_pData = new char[strlen(str.m_pData) + 1];
    //Copy the parameter to the newly allocated memory
    strcpy(m_pData, str.m_pData);
}
else
{
    m_pData = 0;
}
return *this;
}
4

2 回答 2

1

在您的复制构造函数中

//Copy Constructor


List::List(const List& str)
{

    if(Empty() == true)
    {
        m_pData = 0;
        return;
    }
    m_pData = str.m_pData;
  strcpy(m_pData, str.m_pData);
}

您正在使用 Empty()bool Empty() const {return m_pFront == 0;}并且 m_pFront 到目前为止尚未初始化。

而且m_pData = str.m_pData不需要strcpy。

而是复制此字符串(首先分配然后复制)或使用 std::string 代替。

于 2013-08-29T03:35:52.830 回答
1

可以 char* m_pData;改为std::string m_sData,否则需要手动为 m_pData 分配内存。

并且m_pData = str.m_pData;会导致问题,因为您让 m_pData 指向 str.m_pData,如果 str.m_pData 得到删除它。m_pData 指向未知的地方。

于 2013-08-29T03:34:29.947 回答