我正在爲一個簡單的「節點」類寫一些C++代碼。這基本上是一個用來管理線性鏈表的類。我通常使用結構執行此操作,但我試圖更好地處理OOP和類。 (注意:String類是我的典型「字符串」類的版本(修剪下來),它實現了一個拷貝構造函數,賦值重載,析構函數等等。在測試中,偉大的工作,似乎完全獨立的):似乎從複製構造函數中獲取段錯誤?
class Node {
public:
//Constructor
//-----------
Node() : next_(0) {} //inline (String constructor called)
//Destructor
//----------
~Node();
//Copy Constructor
//----------------
Node(const Node &);
//Operator Overload: =
//---------------------
//In conjunction with copy constructor. Protects Class.
Node & operator=(const Node &);
private:
String relatedEntry_;
Node * next_;
};
創建一個實例正常工作(即Node node;
),但是當我創建調用拷貝構造函數實例我在節目的最後結了段錯誤,因爲它正在清理。使用鏈接列表和類的結構之間的區別與我玩一些技巧,我想我在這裏丟失了一些關鍵。下面是默認的構造函數的實現,拷貝構造函數和重載賦值運算符:
//Constructor inlined
//Destructor
Node::~Node()
{
Node * curr = next_;
while (curr) //cycle through LL and delete nodes
{
Node * temp = curr; //hold onto current
curr = curr->next_; //increment one
delete temp; //delete former current
}
}
//Copy Constructor
Node::Node(const Node & cp)
{
std::cout << "in CopyCon" << std::endl;
relatedEntry_ = cp.relatedEntry_; //calls String class copy constructor/assignment overload
Node * curr = cp.next_; //for clarity, for traversal
while (curr) //copies related entry structure
{
Node * oldNext = next_;
next_ = new Node;
next_->next_ = oldNext; //'next' field (assign prior)
next_->relatedEntry_ = curr->relatedEntry_; //String class copy
curr = curr->next_; //increment
}
}
//OO: =
Node & Node::operator=(const Node & cp)
{
std::cout << "in OO: =" << std::endl;
if (this == &cp)
return *this; //self assignment
delete next_; //delete LL
relatedEntry_ = cp.relatedEntry_; //String Class Assignment Overload
Node * curr = cp.next_; //for clarity, for traversal
while (curr)
{
Node * oldNext = next_; //hold onto old
next_ = new Node;
next_->next_ = oldNext; //set next to old
next_->relatedEntry_ = curr->relatedEntry_; //set this string to cp string
curr = curr->next_; //increment
}
return *this;
}
注意使用重載定義功能似乎工作正常(無段錯誤),儘管它幾乎相同的代碼...我假設它與兩個對象在之前已經初始化這一事實有關,那麼分配發生了嗎?
//This seems to work ok
Node node1;
Node node2;
node2 = node1;
我一直在這個bug幾個小時,我得休息一下。我真的很感激任何見解。謝謝。
在發生崩潰,分段錯誤或任何其他問題時,您應該做的第一件事是在調試器中運行您的程序。它不僅可以幫助您找到崩潰的位置,還可以讓您檢查變量以幫助您找出原因。 – 2012-07-31 07:06:39
下一步應該在'Node * oldNext = next_; //保持舊''? – irrelephant 2012-07-31 07:09:06
我不同意reark「_it幾乎相同的code_」。您的operator =的代碼與隱式代碼不是同一個代碼:您對整個列表進行深層次的複製,而默認賦值操作符只是對該對象進行淺度複製(字符串和指針的副本_next。 – Bentoy13 2012-07-31 07:16:30