2016-11-17 30 views
0

所以我編碼瀏覽器選項卡結構,並且我有5個自定義類,Stack,LinkedList,節點(用於鏈接列表),選項卡和瀏覽器。 LinkedList和Stack自己可以正常工作,但是當我在Browser的構造函數中構造一個LinkedList時,我得到錯誤。所以在我的主,我只調用瀏覽器()下面是代碼:自定義類構造中的分段錯誤

LinkedList<T>::LinkedList(){ 
    front=NULL; 
    back=NULL; 
}` 

Node<T>::Node(){ 
    prev=NULL; 
    next=NULL; 
    data=T(); 
} 

Stack<T>::Stack(int capacity){ //capacity is optional, this is the default constructor. 
     this->capacity=capacity; 
     this->size=0; 
     this->items=new T[capacity]; 
} 

`

Browser(){ 
      selected = NULL; 
      //print browser link construction starts 
      pages= LinkedList<Tab>(); //This line gives the error. 
      closedPages= Stack<Tab>(); 
      curr_index=-1; 
      tab_count=0; 
} 

Tab(){ 
    current_page=""; 
    prevPages=Stack<string>(); 
    nextPages=Stack<string>(); 
    closed_index=-1; 
} 

什麼是更有趣的是,當我這樣做打印插入,我看到的是在第一次啓動並完成一個鏈接建設(之間沒有做任何標籤建設),然後做一堆棧和標籤建設,然後打印「瀏覽器鏈接建設啓動」,然後進入並完成另一個鏈接建設,然後給出seg故障。因此,它連接兩次建築甚至強硬,我希望它只能做一次?

謝謝,並提前抱歉,如果它是極其簡單/愚蠢的東西。

EDIT1:全LinkedList的代碼

#ifndef _LINKEDLIST_H_ 
#define _LINKEDLIST_H_ 

#include <iostream> 
#include <cstddef> 
#include <stdexcept> 
#include "Node.hpp" 

using namespace std; 

template <class T> 
class LinkedList { 
    private: 
     /* pointer to the first node */ 
     Node<T>* front; 
     /* pointer to the last node */ 
     Node<T>* back; 

    public: 

     LinkedList(); 
     LinkedList(const LinkedList<T>& ll); 
     LinkedList<T>& operator=(const LinkedList<T>& ll); 
     ~LinkedList(); 

     /* returns the first node of the linked list */ 
     Node<T>& getFront() const; 
     /* returns the last node of the linked list */ 
     Node<T>& getBack() const; 
     /* returns the node in given "pos"ition of the linked list */ 
     Node<T>& getNodeAt(int pos) const; 
     /* returns the pointer of the node in given 
      "pos"ition of the linked list */ 
     Node<T>* getNodePtrAt(int pos) const; 

     /* inserts a new node containing "data" 
      after the node "prev" 
      */ 
     void insert(Node<T>* prev, const T& data); 
     /* inserts a new node containing "data" 
      at "pos"ition in the linked list 
      */ 
     void insertAt(int pos, const T& data); 
     /* erases the given "node" from the linked list */ 
     void erase(Node<T>* node); 
     /* erases the node in given "pos"ition from the linked list */ 
     void eraseFrom(int pos); 
     /* clears the contents of the linked list */ 
     void clear(); 

     /* inserts a new node containing "data" 
      to the front of the linked list 
      */ 
     void pushFront(const T& data); 
     /* inserts a new node containing "data" 
      to the back of the linked list 
      */ 
     void pushBack(const T& data); 

     /* removes the first node */ 
     void popFront(); 
     /* removes the last node */ 
     void popBack(); 

     /* returns true if the list is empty, false otherwise */ 
     bool isEmpty() const; 
     /* returns the number of items in the list */ 
     size_t getSize() const; 
     /* prints the contents of the linked list 
      one node data per line 
      assumes the objects in the node have operator<< overloaded 
      */ 
     void print() const; 

}; 


template <class T> 
LinkedList<T>::LinkedList(){ 
    cout << "list construction starts" << endl; 
    front=NULL; 
    back=NULL; 
    cout << "list construction ends" << endl; 
} 

//COPY AND ASSIGMENT 
template<class T> 
LinkedList<T>::LinkedList(const LinkedList<T>& ll){ 
    *this=ll; 
} 
template<class T> 
LinkedList<T>& LinkedList<T>::operator=(const LinkedList<T>& ll){ 
    clear(); 
    Node<T>* temp=&(ll.getFront()); 
    while(temp->getNext()!=NULL){ 
    pushBack(temp->getData()); 
    temp->setNext(temp->getNext()); 
    } 
    pushBack(temp->getData()); 
    return *this; 
} 
template<class T> 
LinkedList<T>::~LinkedList(){ 
    clear(); 
} 

template <class T> 
Node<T>& LinkedList<T>::getFront() const{ 
    return *front; 
} 
template <class T> 
Node<T>& LinkedList<T>::getBack() const{ 
    return *back; 
} 
template <class T> 
Node<T>& LinkedList<T>::getNodeAt(int pos) const{ 
    if(pos<0 or pos>=getSize()){ 
    throw out_of_range("Bad Input"); 
    } 
    Node<T>* retval=front; 
    for(int i=0;i<pos;i++){ 
     retval=retval->getNext(); 
    } 
    return *retval; 
} 
template <class T> 
Node<T>* LinkedList<T>::getNodePtrAt(int pos) const{ 
    if(pos<0 or pos>getSize()){ 
    throw out_of_range("Bad Input"); 
    } 
    Node<T>* retval=front; 
    for(int i=0;i<pos;i++){ 
     retval=retval->getNext(); 
    } 
    return retval; 
} 

template <class T> 
void LinkedList<T>::insert(Node<T>* prev,const T& data){ 
    if(prev==NULL){ 
    pushFront(data); 
    } 
    else if(prev==back){ 
    pushBack(data); 
    } 
    else{ 
    Node<T>* newNode=new Node<T>(); 
    newNode->setData(data); 
    prev->getNext()->setPrev(newNode); 
    newNode->setNext(prev->getNext()); 
    newNode->setPrev(prev); 
    prev->setNext(newNode); 
    } 
} 
template <class T> 
void LinkedList<T>::insertAt(int pos,const T& data){ 
    if(pos==0){ 
    pushFront(data); 
    } 
    else if(pos==getSize()){ 
    pushBack(data); 
    } 
    else{ 
    Node<T>* tmp=getNodePtrAt(pos); 
    Node<T> newNode; 
    newNode.setData(data); 
    tmp->getPrev()->setNext(&newNode); 
    newNode.setNext(tmp); 
    newNode.setPrev(tmp->getPrev()); 
    tmp->setPrev(&newNode); 
    } 

} 


template <class T> 
void LinkedList<T>::pushFront(const T& data){ 
    Node<T>* newNode=new Node<T>(); 
    newNode->setData(data); 
    if(front==NULL){ 
    front=newNode; 
    back=newNode; 
    } 
    else { 
    newNode->setNext(front); 
    front->setPrev(newNode); 
    front=newNode; 
    newNode->setPrev(NULL); 
    } 
} 

template <class T> 
void LinkedList<T>::pushBack(const T& data){ 
    Node<T>* newNode=new Node<T>(); 
    newNode->setData(data); 
    if(front==NULL){ 
    front=newNode; 
    back=newNode; 
    } 
    else { 
    newNode->setPrev(back); 
    back->setNext(newNode); 
    back=newNode; 
    newNode->setNext(NULL); 
    } 

} 

template <class T> 
void LinkedList<T>::erase(Node<T>* node){ 
    if(node==front){ 
    popFront(); 
    } 
    if(node==back){ 
    popBack(); 
    } 
    else { 
    node->getNext()->setPrev(node->getPrev()); 
    node->getPrev()->setNext(node->getNext()); 
    node->setNext(NULL); node->setPrev(NULL); 
    } 
    delete node; 

} 
template <class T> 
void LinkedList<T>::eraseFrom(int pos){ 
Node<T>* tmp=getNodePtrAt(pos); 
erase(tmp); 
} 
template <class T> 
void LinkedList<T>::clear(){ 
    while(!isEmpty()){ 
    popFront(); 
    } 
} 


template <class T> 
void LinkedList<T>::popFront(){ 
    Node<T>* tmp; 
    tmp=front; 
    if(front==back){ 
    front=NULL; 
    back=NULL; 
    } 
    else{ 
    front=front->getNext(); 
    front->setPrev(NULL); 
    } 
    delete tmp; 
} 
template <class T> 
void LinkedList<T>::popBack(){ 
    Node<T>* tmp; 
    tmp=back; 
    if(front==back){ 
    front=NULL; 
    back=NULL; 
    } 
    else { 
    back=back->getPrev(); 
    back->setNext(NULL); 
    } 
    delete tmp; 
} 

template <class T> 
bool LinkedList<T>::isEmpty() const{ 
    return front==NULL; 
} 
template <class T> 
size_t LinkedList<T>::getSize() const{ 
    if(front==NULL){ 
    return 0; 
    } 
    size_t size=1; 
    Node<T>* current=front; 
    while(current->getNext()!=NULL){ 
    size++; 
    current=current->getNext(); 
    } 
    return size; 
} 

template <class T> 
void LinkedList<T>::print() const{ 
    Node<T>* current=front; 
    if(front!=NULL){ 
    while(current->getNext()!=NULL){ 
     cout << current->getData() << endl; 
     current=current->getNext(); 
    } 
    cout << current->getData() << endl; 
    } 
} 

#endif 
+0

我們可以看到'LinkedList'實現的其餘部分嗎? –

+0

你已經發布了太多的代碼。也許縮小你的代碼將幫助你找到錯誤。 – erip

+1

你是否需要實現自己的鏈表和棧?標準庫已經提供了這些。站在巨人的肩膀上。這就是他們在那裏的原因。 – user4581301

回答

3

你的賦值運算符似乎是有問題的。在您的瀏覽器構造函數中,我看到:

pages = LinkedList<Tab>(); 

這實際上是不必要的,但會觸發您的問題。您的「網頁」變量已經默認構建(這就是爲什麼您在瀏覽器構造函數代碼開始的時候才能看到打印語句之前的所有活動)。然後你執行利用你的賦值操作符的賦值。

您的賦值運算符假定源LinkedList對象有數據,但它沒有。它調用getFront(),它取消引用'front',它是NULL。你應該首先檢查它是否爲空,我發現你已經有了一個方法(isEmpty())。

+0

長話短說,'operator ='有多個問題。 –

+0

謝謝我的猜測,我仍然無法完全理解它,但現在我刪除了該行並繼續調試,希望它能起作用。 但是,我的私人成員的情況是:LinkedList pages; Stack closedPages;而我在構造函數中強硬我會執行它們,所以我得到的是我不必? –

+0

C++對象的類成員(例如您提到的)會使用它們的默認構造函數自動構造,因此您不需要像在所提供的代碼中那樣爲它們分配任何值。你真的只需要初始化不是C++對象的類成員(像ints,指針,布爾等等的原語)的值。此外,初始化可以(並且通常)在構造函數初始化程序列表中完成(體面參考:http://stackoverflow.com/questions/926752/why-should-i-prefer-to-use-member-initialization-list ) – qexyn