0

我正在處理賦值,我必須在C++中爲給定的鏈表創建一個深層拷貝構造函數。拷貝構造函數中的代碼和指針做工精細,但在打印功能被調用我在第59行中得到的功能分割故障:C++方法調用的分段錯誤

cout << v->elem << " "; 

我一直在試圖調試這幾個小時,我有不知道爲什麼seg故障發生。工作分配的代碼(僅在拷貝構造函數的代碼是我的):

#include <iostream> 
#include <stddef.h> 
#include "intSLinkedList.h" 

using namespace std; 

intSLinkedList::intSLinkedList(const intSLinkedList& other){ 
    if(other.head == NULL){ 
     this->head = NULL; 
    } 

    else{ 
     intSNode* src_cursor = other.head; 

     while(src_cursor != NULL){ 
      this->addFront(src_cursor->elem); 
      src_cursor = src_cursor->next; 
     } 
    } 
} 

intSLinkedList::intSLinkedList(): head(NULL), tail(NULL) { } 

bool intSLinkedList::empty() const{ return head == NULL; } 

const int& intSLinkedList::front() const{ return head->elem; } 

//intSLinkedList::~intSLinkedList(){ while (!empty()) removeFront(); } 

void intSLinkedList::addFront(const int& e) {// add to front of list 
    intSNode* v = new intSNode;   // create new node 
    v->elem = e;       // store data 
     v->next = head;      // head now follows v 
     head = v;       // v is now the head 
    if (head->next==NULL) 
     tail = head; 
} 

void intSLinkedList::addBack(const int& e) {// add to front of list 
     intSNode* v = new intSNode;   // create new node 
     v->elem = e; 
    v->next = NULL;       // store data 

    tail->next = v;      // head now follows v 
    tail = v; 
} 

void intSLinkedList::removeFront() {   // remove front item 
     intSNode* old = head;    // save current head 
     head = old->next;     // skip over old head 
     delete old;       // delete the old head 
} 

void intSLinkedList::print() { 
     intSNode* v = head; 
     while (v != NULL){ 
      cout << v->elem << " "; 
      v = v->next; 
    } 
    cout << endl; 
} 

int intSLinkedList::count() { 
     intSNode* v = head; 
    int n = 0; 
     while (v != NULL){ 
     n++; 
       v = v->next; 
     } 
    return n; 
} 

頭文件:

class intSLinkedList; 

class intSNode { 
private: 
    int elem; 
    intSNode* next;  
    friend class intSLinkedList; 
}; 

class intSLinkedList { 
public: 
    intSLinkedList(); 

    intSLinkedList(const intSLinkedList& other); 

    bool empty() const; 

    const int& front() const; 

    //~intSLinkedList(); 

    void addFront(const int& e); 

    void addBack(const int& e); 

    void removeFront(); 

    void print(); 

    int count(); 

private: 
    intSNode* head; 
    intSNode* tail; 
}; 

和測試文件:

#include <iostream> 
#include "intSLinkedList.h" 

using namespace std; 

int main(){ 
    intSLinkedList int_sll; 
    int_sll.addFront(5); 
    int_sll.addFront(12); 
    int_sll.addFront(6); 
    cout << "int_sll : "; 
    int_sll.print(); 

    intSLinkedList int_sll2 = int_sll; 
    cout << "int_sll2 : "; 
     int_sll2.print(); 

    int_sll.addBack(100); 
    cout << "int_sll : "; 
    int_sll.print(); 
    cout << "int_sll2 : "; 
     int_sll2.print(); 

} 

我GDB的輸出:

int_sll : 6 12 5 

Program received signal SIGSEGV, Segmentation fault. 
0x0000000000400cce in intSLinkedList::print (this=0x7fffffffe010) 
    at intSLinkedList.cpp:57 
57    cout << v->elem << " "; 

任何幫助或指向正確的方向非常感謝。

+0

你已經打開了你的調試器。嘗試逐句通過導致故障的代碼。 – tadman

+0

最有可能的是,'v'是NULL。 – wallyk

+0

如果tail是nullptr,你的addBack將會崩潰 –

回答

2

看起來你沒有用任何東西初始化next,所以它是一個未定義的值,當你導航到它時你會崩潰。

你不顯示你所有的構造變種所以要確保每個構造總是初始化所有屬性。