类模板 - 从"box"对象中删除项

Class templates - Removing an item from a "box" object

本文关键字:对象 删除 box      更新时间:2023-10-16

我把要点贴在这里:

这是一门数据结构课程。我们正在创建一个简单的PlainBox类并创建成员函数,这些成员函数允许我们从"box"中添加和删除项,检查框是否为空,以及检查私有数据成员的值。我已经完成了作业中的所有内容,除了一件事:我不确定如何创建remove()函数,以便我们可以从已经包含内容的框中删除项。

下面是说明:"向模板类添加一个名为"remove"的公共方法。此方法将没有参数并返回bool值。如果盒子中有一个项目,该项目应该被删除(使盒子为空),该方法应该返回true(任务完成)。否则,不修改框并返回false(任务无法执行)。"

remove()函数在PlainBox.cpp文件中。我只是困惑如何重置"ItemType项目"变量为默认值,使框为空。我应该把'item'设置为空字符串吗?(item = " ";)

template<class ItemType>
bool PlainBox<ItemType>::remove()
{
    if (full == true)
    {
        full = false;
        return true;
    }
    else
    {
        return false;
    }
}

在main.cpp文件的底部,我正在测试secondNumberBox上的remove()函数,然后检查私有成员变量是否为空。

下面是供参考的PlainBox类:
// Declaration for the class PlainBox
class PlainBox: public BoxInterface<ItemType>   // added parent class
{
private:
   // Data field
   ItemType item;
   bool full;
public:
   // Default constructor
   PlainBox();
   // Parameterized constructor
   PlainBox(const ItemType& theItem);
   // Accessor method to get the value of the data field
   ItemType getItem() const;
   // Add method
   bool add(const ItemType& theItem);
   // Remove method
   bool remove();
   // isEmpty method
   bool isEmpty();
}; // end PlainBox

我希望我这篇文章的措辞正确,如果这篇文章没有任何意义,请告诉我!

为什么要弄乱item?您有一个标志(bool full),表示是否使用项目。我相信你有一个方法,写到项目时,标志是假的。如果bool full为假,那么写入方法是否关心item中的内容?