问题描述
所以我尝试使用带有单元格数组的链接数据结构在 C++ 中实现 Bag ADT。我已经编辑了 ArrayLinkedBag.cpp 和头文件,但是我一直遇到奇怪的错误,我想知道是否有人可以帮助我找出我出错的地方。当我尝试进行测试时,由于某种原因整个过程中止,我无法查明错误。我已经尝试了一切来查明问题,包括重写所有内容并逐行测试行,但它只是不起作用。任何帮助将不胜感激。
ArrayLinkedBag.cpp
/** Implementation file for the class ArrayLinkedBag.
@file ArrayLinkedBag.cpp */
#include "ArrayLinkedBag.h"
#include <cstddef>
template<class ItemType>
ArrayLinkedBag<ItemType>::ArrayLinkedBag(): head(-1),free(0),itemCount(0)
{
//create free list
for (int i = 0; i < DEFAULT_CAPACITY; i++)
{
cells[i] = (Cell<ItemType>){"a",i+1};
}
} // end default constructor
template<class ItemType>
int ArrayLinkedBag<ItemType>::getCurrentSize() const
{
return itemCount;
} // end getCurrentSize
template<class ItemType>
bool ArrayLinkedBag<ItemType>::isEmpty() const
{
return itemCount == 0;
} // end isEmpty
template<class ItemType>
bool ArrayLinkedBag<ItemType>::add(const ItemType& newEntry)
{
bool hasRoomToAdd = (itemCount < DEFAULT_CAPACITY);
if (hasRoomToAdd)
{
int newFree=cells[free].next;
cells[free]=(Cell<ItemType>){newEntry,head};
head=free;
itemCount++;
free=newFree;
} // end if
return hasRoomToAdd;
} // end add
template<class ItemType>
bool ArrayLinkedBag<ItemType>::remove(const ItemType& anEntry)
{
int locatedindex = getIndexOf(anEntry);
bool canRemoveItem = !isEmpty() && (locatedindex > -1);
if (canRemoveItem)
{
itemCount--;
cells[locatedindex] = (Cell<ItemType>){NULL,free};
free=locatedindex;
} // end if
return canRemoveItem;
} // end remove
template<class ItemType>
void ArrayLinkedBag<ItemType>::clear()
{
//reset free list
for (int i = 0; i < DEFAULT_CAPACITY; i++)
{
cells[i] = (Cell<ItemType>){"a",i+1};
}
head=-1;
free=0;
itemCount = 0;
} // end clear
template<class ItemType>
bool ArrayLinkedBag<ItemType>::contains(const ItemType& anEntry) const
{
return getIndexOf(anEntry) > -1;
} // end contains
template<class ItemType>
int ArrayLinkedBag<ItemType>::getFrequencyOf(const ItemType& anEntry) const
{
int frequency = 0;
int curIndex = 0; // Current array index
while (curIndex < itemCount)
{
if (cells[curIndex].item == anEntry)
{
frequency++;
} // end if
curIndex++; // Increment to next entry
} // end while
return frequency;
} // end getFrequencyOf
template<class ItemType>
std::vector<ItemType> ArrayLinkedBag<ItemType>::toVector() const
{
std::vector<ItemType> bagContents;
for (int i = 0; i < itemCount; i++)
bagContents.push_back(cells[i].item);
return bagContents;
} // end toVector
// private
template<class ItemType>
int ArrayLinkedBag<ItemType>::getIndexOf(const ItemType& target) const
{
bool found = false;
int result = -1;
int searchIndex = 0;
// If the bag is empty,itemCount is zero,so loop is skipped
while (!found && (searchIndex < itemCount))
{
if (cells[searchIndex].item == target)
{
found = true;
result = searchIndex;
}
else
{
searchIndex++;
} // end if
} // end while
return result;
} // end getIndexOf
ArrayLinkedBag.h
/** Header file for an array-based linked list implementation of the ADT bag.
@file ArrayLinkedBag.h */
#ifndef ARRAY_LINKED_BAG_
#define ARRAY_LINKED_BAG_
#include "BagInterface.h"
template<class ItemType>
class ArrayLinkedBag : public BagInterface<ItemType>
{
private:
static const int DEFAULT_CAPACITY = 6;
template<class T>
struct Cell {
T item;
int next;
};
Cell<ItemType> cells[DEFAULT_CAPACITY];
int head;
int itemCount; // Current count of bag items
int free;
// Returns either the index of the element in the array items that
// contains the given target or -1,if the array does not contain
// the target.
int getIndexOf(const ItemType& target) const;
public:
ArrayLinkedBag();
int getCurrentSize() const;
bool isEmpty() const;
bool add(const ItemType& newEntry);
bool remove(const ItemType& anEntry);
void clear();
bool contains(const ItemType& anEntry) const;
int getFrequencyOf(const ItemType& anEntry) const;
std::vector<ItemType> toVector() const;
};
#include "ArrayLinkedBag.cpp"
#endif
错误信息
g++ -Wall -Wextra -g src/test_driver.cpp -o build/test_driver
In file included from src/test_driver.cpp:7:
src/ArrayLinkedBag.cpp: In instantiation of ‘ArrayLinkedBag<ItemType>::ArrayLinkedBag() [with ItemType = std::__cxx11::basic_string<char>]’:
src/test_driver.cpp:91:48: required from here
src/ArrayLinkedBag.h:24:9: warning: ‘ArrayLinkedBag<std::__cxx11::basic_string<char> >::free’ will be initialized after [-Wreorder]
int free;
^~~~~
src/ArrayLinkedBag.h:22:9: warning: ‘int ArrayLinkedBag<std::__cxx11::basic_string<char> >::head’ [-Wreorder]
int head;
^~~~
In file included from src/ArrayLinkedBag.h:43,from src/test_driver.cpp:7:
src/ArrayLinkedBag.cpp:8:1: warning: when initialized here [-Wreorder]
ArrayLinkedBag<ItemType>::ArrayLinkedBag():free(0),head(0),itemCount(0){
^~~~~~~~~~~~~~~~~~~~~~~~
[username testassignment3]$ make test
terminate called after throwing an instance of 'std::logic_error'
what(): basic_string::_M_construct null not valid
/bin/sh: line 1: 98661 Aborted (core dumped) ./build/test_driver ARRAY_LINKED > output/array_linked.out
make: *** [test] Error 134
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)