2012-12-01 3 views
1

기본적으로 작은 서점 저장소 응용 프로그램 인 C++ 클래스 용 프로그램을 작성 중입니다. 서점은 회원 목록 (ListMemberType)과 연결된 도서 목록 (ListBookType)이 있어야합니다. 연결된 목록의 각 노드는 다음 구성 요소 및 구성 요소에 대한 링크로 구성됩니다. 구성 요소는 BookType 유형입니다. 여기 연결된 목록 구성 요소를 반환 할 때의 Seg 오류

는 ListBookType

#include "bookType.h" 

typedef BookType ItemType; // Type of each component 
struct NodeType;    // Forward declaration 

class ListBookType { 
public: 
    const ListBookType& operator=(const ListBookType& rightObject); 
    //overload assignment operator 
    //will set the left side equal to right side 
    void Replace(ItemType theNewItem, ItemType theOldItem); 
    //Pre:These are the same book(ISBN is the same) 
    //Post: old component will be replaced with the new component 
    ListBookType(); 
    // Constructor 
    // Post: Empty list has been created 
    ~ListBookType(); 
    // Destructor 
    // Post: All nodes are returned to the heap 
    ListBookType(const ListBookType& otherList); 
    // Copy constructor 
    // Post: A deep copy of otherList is created and dataPtr is the 
    //  external pointer to this copy 

    // Action respnsibilities 
    void Insert(ItemType item); 
    // Pre: List is not full and item is not in the list 
    // Post: item is in the list and length has been incremented 
    void Delete(ItemType item); 
    // Post: item is not in the list 
    void ResetList(); 
    // The current position is reset to access the first item in the list 
    ItemType GetNextItem(); 
    // Assumptions: No transformers are called during the iteration. 
    // There is an item to be returned; that is, HasNext is true when 
    // this method is invoked 
    // Pre: ResetList has been called if this is not the first iteration 
    // Post: Returns item at the current position. 

    // Knowledge responsibility 
    int GetLength() const; 
    // Post: Returns the length of the list 
    bool IsEmpty() const; 
    // Post: Returns true if list is empty; false otherwise 
    bool IsFull() const; 
    // Post: Returns true if list if full; false otherwise 
    bool IsThere (ItemType item) const; 
    // Post: Returns true if item is in the list and false otherwise 
    bool HasNext() const; 
    // Returns true if there is another item to be returned; false 
    // otherwise 
    ItemType GetBook(ItemType bookToGet)const; 
    //Pre: Book is in list 
    //Post: the book is returned 
    //Pre: Book is in the list 
    //Post: Book with matching ISBn will be returned 
    private: 
    NodeType* dataPtr;  // Pointer to the first node in the list 
    int length; 
    NodeType* currentPos; // Pointer to the current position in a traversal 
    NodeType* lastPtr; 
}; 

에 대한 헤더 파일입니다 그리고 여기 내가 오류를 일으킬 수있는 생각 부품 함께 내 오류가 알고있는 사양 파일의 일부이다.

#include "listBookType.h" 
#include "bookType.h" 
#include <iostream> 
#include <cstddef>   // For NULL 

using namespace std; 

typedef NodeType* NodePtr; 
struct NodeType { 
    ItemType component; 
    NodePtr link; 
}; 

const ListBookType& ListBookType::operator=(const ListBookType& rightObject) { 
    cout<<"Assignment operator bookList"<<endl; 

    NodePtr fromPtr; // Pointer into list being copied from 
    NodePtr toPtr;  // Pointer into new list being built 
    if(this != &rightObject) { 
     if (rightObject.dataPtr == NULL) { 
     dataPtr = NULL; 
     return *this; 
     } 
     // Copy first node 
     fromPtr = rightObject.dataPtr; 
     dataPtr = new NodeType; 
     dataPtr->component = fromPtr->component; 
     // Copy remaining nodes 
     toPtr = dataPtr; 
     fromPtr = fromPtr->link; 
     while (fromPtr != NULL) 
     // Copying nodes from original to duplicate 
     { 
     toPtr->link = new NodeType;  // Store new node in link of last 
               // node added to new list 
     toPtr = toPtr->link;   // toPtr points to new node 
     toPtr->component = fromPtr->component; // Copy component to new node 
     fromPtr = fromPtr->link;  // fromPtr points to next node 
         // of original list 
     } 
     toPtr->link = NULL; 
     lastPtr = toPtr;  // Set last pointer 
    } 
    return *this; 
} 


ItemType ListBookType::GetBook(ItemType bookToGet)const { 
    NodePtr currPtr = dataPtr;  // Loop control pointer 
    NodePtr tempPtr = NULL; 

    while (currPtr != NULL && currPtr->component != bookToGet 
     && currPtr->component < bookToGet) { 
    tempPtr = currPtr; 
    currPtr = currPtr->link; 
    } 

    cout<<"right before return of getBook"<< endl; 

    return tempPtr->component; 
} 

ListBookType::ListBookType(const ListBookType& otherList) { 
    cout<<"copy construct book list"<< endl; 
    NodePtr fromPtr; // Pointer into list being copied from 
    NodePtr toPtr;  // Pointer into new list being built 

    if (otherList.dataPtr == NULL) { 
     dataPtr = NULL; 
     return; 
    } 
    // Copy first node 
    fromPtr = otherList.dataPtr; 
    dataPtr = new NodeType; 
    dataPtr->component = fromPtr->component; 
    // Copy remaining nodes 
    toPtr = dataPtr; 
    fromPtr = fromPtr->link; 
    while (fromPtr != NULL) { // Copying nodes from original to duplicate 
     toPtr->link = new NodeType;  // Store new node in link of last 
         // node added to new list 
     toPtr = toPtr->link;   // toPtr points to new node 
     toPtr->component = fromPtr->component; // Copy component to new node 
     fromPtr = fromPtr->link;  // fromPtr points to next node 
         // of original list 
    } 
    toPtr->link = NULL; 
    lastPtr = toPtr;  // Set last pointer 
} 



ListBookType::~ListBookType() { 
    cout<< "destructor book list"<< endl; 
    NodePtr tempPtr; 
    NodePtr currPtr = dataPtr; 
    while (currPtr != NULL) { 
     tempPtr = currPtr; 
     currPtr = currPtr->link; 
     delete tempPtr; 
    } 
} 

내가 겪고있는 문제는 GetBook(ItemType bookToGet)입니다. 나는 문제를 추적하고 나는 tempPtr->component를 돌려 보낼 때 잘못하고있다. 코드의 다른 몇 곳에서이 동일한 문제가 발생하며 왜 여기에 오류가 있는지 또는 근본적인 문제가 무엇인지 알 수 없습니다. (참고 : BookType 클래스에는 할당 연산자가 오버로드되거나 복사 생성자가 필요한 동적 데이터가 포함되어 있지 않습니다.)

정말 도움이됩니다. 나는 내가 모르는 중요한 것을 놓친 것 같아. while 문에서 조건이 평가에 처음 거짓이면

답변

0

은 다음 tempPtrNULL 이외로 설정하고, return 문에 tempPtr->component는 널 포인터와 충돌 역 참조하려고 않을 것입니다.

처음으로 while 조건이 거짓이고 tempPtrNULL 인 경우 함수가 반환 할 합리적인 기본값이 무엇인지 생각해보십시오. 그 대신 기본값을 반환하십시오.

2

GetBook 루틴에서는 dataPtr이 이미 NULL 인 경우를 고려합니다.

return tempPtr->component;에 액세스 중이며 segfault가 발생합니다.

dataPtr이 별도로 NULL 인 경우 대소 문자를 처리해야합니다.

while (currPtr != NULL && currPtr->component != bookToGet // dataPtr = currPtr is NULL 
     && currPtr->component < bookToGet){ 
    tempPtr = currPtr; 
    currPtr = currPtr->link; 
} 
cout<<"right before return of getBook"<< endl; 
return tempPtr->component; // SEGFAULT! 
+0

감사합니다. 그것이 문제였습니다. 나는 고칠 수있어서 기쁘다. – Ryan

관련 문제