1

基本的に小さな書店アプリケーションである C++ クラスのプログラムに取り組んでいます。ブック ストアには、メンバーのリンク リスト (ListMemberType) とブックのリンク リスト (ListBookType) があります。リンク リストの各ノードは、次のコンポーネントへのリンクとコンポーネントで構成されます。コンポーネントのタイプは BookType です。

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 クラスには、代入演算子のオーバーロードまたはコピー コンストラクターを必要とする動的データは含まれていません)

何か助けていただければ幸いです。自分の知らない大切なものを見落としているような気がします。

4

2 に答える 2