我正在为我的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类不包含任何需要赋值运算符重载的动态数据或复制构造函数)
我真的很感激任何帮助。我觉得我只是缺少一些我不了解的重要事项。
答案 0 :(得分:2)
在GetBook
例程中,考虑dataPtr
已经为NULL的情况。
您将访问return tempPtr->component;
//导致段错误。
当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!
答案 1 :(得分:0)
如果第一次评估while
语句中的条件为false,则tempPtr
永远不会设置为NULL
以外的任何内容,tempPtr->component
在return语句中尝试取消引用空指针并崩溃。
如果while
条件第一次为假,然后在返回语句之前tempPtr
为NULL
,请考虑函数返回的合理默认值是什么返回默认值。