2013-08-29 95 views
0

我已经有人工投入它的列表的程序,它只是一堆名字扔在这样的复制当我尝试通过我的拷贝构造函数和赋值运算符运行已经创建的列表时,列表就会出现在“”(在isEmpty函数中定义)C++列表不是通过复制构造函数或赋值运算符

忽略所有“TODO”注释点,我。

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

下面是相关代码:

List.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; 

}; 

List.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; 
} 
+0

你的类没有构造,你正在使用Empty()== true,它不会给出明确的结果。 – doptimusprime

+0

尝试'str.Empty()'而不是 – user1520427

+0

str.Empty()只会导致程序崩溃,它不会在之前。 –

回答

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指向未知的地方。

+0

做完你的建议后,我的整个程序在启动时崩溃,它以前没有。 –

+0

你会发布你的新代码吗? –

+0

我恢复了我的代码,因为据我怀疑(和大多数网站说),我确实需要strcpy。我所修复的是手动内存分配,但是他们仍然显示空列表。 –

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); 
} 

您正在使用空(),这是bool Empty() const {return m_pFront == 0;}和m_pFront没有到目前为止初始化。

m_pData = str.m_pData不需要strcpy。

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

相关问题